Python/ Beautiful Soup Data Displaying Issue

77 views Asked by At

I am trying to pull some data from a website. Once I checked the data that I pulled with beuatifulsoup (using print(soup) in the code below) does not seem very well. It is different than once I check with view-source:URL. I am unable to find the fields that I am looking for. Could you please help me to find a solution?

Website: https://www.wayfair.com/furniture/pdp/mercury-row-stalvey-contemporary-4725-wide-1-drawer-server-w003245064.html

Basically, I am trying to get price of this product. I used the same code structure on other websites, it worked properly but it is not working on wayfair.

The second thing that I could not find a solution yet is the last line of my code (StyledBox-owpd5f-0 PriceV2__StyledPrice-sc-7ia31j-0 lkFBUo pl-Price-V2 pl-Price-V2--5000). Instead of name of the product is there a way to get only price like $389.99?

Thanks in advance!

This my code:

html = requests.get('https://www.wayfair.com/furniture/pdp/mercury-row-stalvey-contemporary-4725-wide-1-drawer-server-w003245064.html')
soup=BeautifulSoup(html.text,"html.parser")
print(soup)
inps=soup.find("div",class_="SFPrice").find_all("input")
for inp in inps:
    print(inp.get("StyledBox-owpd5f-0 PriceV2__StyledPrice-sc-7ia31j-0 lkFBUo pl-Price-V2 pl-Price-V2--5000"))
2

There are 2 answers

4
ILTRENTA On

Try with:

soup.findAll('div', {'class': 'SFPrice'})[0].getText()

Or in a more simple way:

inps=soup.findAll('div', {'class': 'SFPrice'})[0]
inps.getText()

Both return the price for that specific product.

3
e.d.n.a On

Your site example is a client-side rendered page and the original html-data fetched doesn't include the searched for elements (like the div with class "SFPrice").

Check out this question for learning about how to scrape javascript-rendered pages with beautifulsoup in combination with selenium and phantomJS, dryscrape or other options.

Or you could also look at this guide.