首页 > 解决方案 > 网页抓取结果为无

问题描述

我很难从 Nordstrom 网站上提取以下产品价格。

<div class="_1vV3F"><section id="product-page-price-lockup" class=""><span><span class="_1k-6h _30Yxg">Price</span><span id="current-price-string" class="_1ds4c">$745.00</span>

这是我的代码的一部分:

soup1 = BeautifulSoup(page.content, "html.parser")
soup2 = BeautifulSoup(soup1.prettify(), "html.parser")
price = soup2.find(class_ = "_1ds4c")
print(price)

标签: pythonweb-scraping

解决方案


选择方式id=

txt = '''<div class="_1vV3F"><section id="product-page-price-lockup" class=""><span><span class="_1k-6h _30Yxg">Price</span><span id="current-price-string" class="_1ds4c">$745.00</span>'''

soup = BeautifulSoup(txt, 'html.parser')

print(soup.select_one('#current-price-string').text)

印刷:

$745.00

或者:

print(soup.find(id='current-price-string').text)

印刷:

$745.00

推荐阅读