Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用靓汤在课堂上获取内容_Python_Beautifulsoup_Bs4 - Fatal编程技术网

Python 使用靓汤在课堂上获取内容

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

我一直在尝试从一个网站上获取一道菜的价格(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>]
但是得到以下错误: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']