Python 使用靓汤在课堂上获取内容
我一直在尝试从一个网站上获取一道菜的价格(4.99美元),我能够检索到以下信息:Python 使用靓汤在课堂上获取内容,python,beautifulsoup,bs4,Python,Beautifulsoup,Bs4,我一直在尝试从一个网站上获取一道菜的价格(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--styli
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>]
但是得到以下错误:TypeError:应该是字符串或类似字节的对象
有什么简单的方法可以从中提取4.99美元的价值吗?谢谢您的时间。您可以通过
数据tl id
属性找到span,并通过获取其下的所有文本。text
spans = soup.find_all(attrs={"data-tl-id":"Price-ProductOffer"})
[span.text for span in spans]
输出:
spans = soup.find_all(attrs={"data-tl-id":"Price-ProductOffer"})
[span.text for span in spans]
['$4.99', '$4.99']