Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.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 从BeautifulSoup中包含嵌套span标记的span标记中刮取文本_Python_Web Scraping_Beautifulsoup - Fatal编程技术网

Python 从BeautifulSoup中包含嵌套span标记的span标记中刮取文本

Python 从BeautifulSoup中包含嵌套span标记的span标记中刮取文本,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我在谷歌上搜索了很多,但没有找到解决这个问题的完美代码行 如何使用Python的BeautifulSoup库从给定的HTML代码中提取55000.00 卢比。 35,916.00 对于您问题中给出的url,您可以通过以下方式获得价格: import requests from bs4 import BeautifulSoup URL = "https://www.amazon.in/gp/offer-listing/B01671J2I6/ref=dp_olp_afts?ie=UT

我在谷歌上搜索了很多,但没有找到解决这个问题的完美代码行

如何使用Python的BeautifulSoup库从给定的HTML代码中提取55000.00


卢比。
35,916.00

对于您问题中给出的url,您可以通过以下方式获得价格:

import requests
from bs4 import BeautifulSoup

URL = "https://www.amazon.in/gp/offer-listing/B01671J2I6/ref=dp_olp_afts?ie=UTF8&condition=all&qid=1602348797&sr=1-19/"

HEADER = {
    'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.6",
}

page = requests.get(URL, headers=HEADER)
soup = BeautifulSoup(page.content, "html5lib")
price_spans = soup.find_all("span", {"style": "text-decoration: inherit; white-space: nowrap;"})
print([p.getText(strip=True) for p in price_spans])

输出:
['Rs.35916.00','Rs.35916.00','Rs.45000.00']


注意:我已经更改了
HTML
解析器,因此您可能必须首先执行
pip安装html5lib

听起来像
soup.find()
无法找到您要查找的标签。很抱歉,我使用此链接不正确-我还想从列表中提取其他价格。我尝试了您的代码,但仍然出现错误:“NoneType”对象没有属性“get_text()”。如果您觉得我的答案有用,请进行投票和/或接受。