我一直在尝试使用网站上的Beautiful Soup来获取商品的价格(4.99美元)并且能够检索以下内容:
tag= soup.findAll("div", class_='prod-PriceHero')
[<div class="prod-PriceHero"><span class="hide-content display-inline-block-m"><span class="display-inline-block arrange-fit Price Price--stylized u-textColor" data-tl-id="Price-ProductOffer"><span><span class="Price-currency" content="USD" itemprop="priceCurrency">$</span><span class="Price-characteristic" content="4.99" itemprop="price">4</span><span class="Price-mark">.</span><span class="Price-mantissa">99</span></span></span></span><span class="hide-content-m"><span class="display-inline-block arrange-fit Price u-textColor" data-tl-id="Price-ProductOffer"><span><span class="Price-currency">$</span><span class="Price-characteristic">4</span><span class="Price-mark">.</span><span class="Price-mantissa">99</span></span></span></span></div>]
从这里开始,我尝试使用以下代码:
soup=bs(tag,'lxml')
txt=soup.get_text()
print(txt)
但是得到以下错误:TypeError:期望的字符串或类似字节的对象。
有没有简单的方法从中提取$ 4.99的值?谢谢你的时间。
答案 0 :(得分:0)
您可以按data-tl-id
属性查找范围,并按.text
spans = soup.find_all(attrs={"data-tl-id":"Price-ProductOffer"})
[span.text for span in spans]
出:
['$4.99', '$4.99']