-1

I would like to access whether the product is avaliable on this website(mouse product). By using the following code, I expected to get "Sold out". However, I always got "Add to Cart", probably because there is a script below according to the condition. How could I get "Sold out" in the situation? Thank you for your help.

    page = r.get("https://finalmouse.com/collections/museum/products/starlight-12-phantom?variant=39672355324040").content
    soup = bs(page, "html.parser")
    span = soup.find("span", {"id":"AddToCartText"})
    print(span.text)

website screen shot

Romeo Lo
  • 3
  • 2
  • The issue is that the 'Add to Cart' text is dynamically changed to "Sold Out" after the page loads. One way to get the value would be to open the website in a headless browser with something like selenium. This question might help: https://stackoverflow.com/questions/56746181/why-python-output-doesnt-match-html-for-target-website – Sean May 22 '22 at 09:22

1 Answers1

0

As Sean wrote in comment, on this page the html is loaded dynamically, the page is updated via js. To parse such sites you need to connect selenium and webdriver. Code for your case:

from bs4 import BeautifulSoup as bs
from selenium import webdriver

url = "https://finalmouse.com/collections/museum/products/starlight-12-phantom?variant=39672355324040"

driver = webdriver.Chrome()
driver.get(url)

page = driver.page_source
soup = bs(page, "html.parser")
span = soup.find("span", {"id":"AddToCartText"})
print(span.text)

Eagle
  • 168
  • 2
  • 11