Python/Beautiful Soup 数据显示问题

时间:2021-07-22 11:31:44

标签: python beautifulsoup data-scrubbing

我正在尝试从网站中提取一些数据。一旦我检查了我用 beuatifulsoup 提取的数据(在下面的代码中使用 print(soup) )似乎不太好。这与我使用 view-source:URL 检查时不同。我无法找到我正在寻找的字段。 你能帮我找到解决办法吗?

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

基本上,我正在尝试获取此产品的价格。我在其他网站上使用了相同的代码结构,它运行正常,但在wayfair 上不起作用。

我还没有找到解决方案的第二件事是我的代码的最后一行(StyledBox-owpd5f-0 PriceV2__StyledPrice-sc-7ia31j-0 lkFBUo pl-Price-V2 pl-Price-V2--5000)。有没有办法只获得 389.99 美元的价格而不是产品名称?

提前致谢!

这是我的代码:

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 个答案:

答案 0 :(得分:0)

尝试:

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

或者以更简单的方式:

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

两者都返回该特定产品的价格。

答案 1 :(得分:0)

您的站点示例是一个客户端呈现的页面,并且获取的原始 html 数据不包括搜索到的元素(例如具有“SFPrice”类的 div)。

查看 this question 以了解如何使用 beautifulsoup 结合 seleniumphantomJS、dryscrape 或其他选项来抓取 javascript 呈现的页面。

或者你也可以看看this guide

相关问题