循环从<div>带有Beautiful Soup解析的标签



我正试图在一个web抓取脚本中运行一个循环,该脚本使用Beautiful Soup从该页面提取数据。循环将遍历每个div标记并提取4条不同的信息。它搜索一个h3、一个div和两个span标记。但是当我加上";。文本";option我从"date"、"soldprice"one_answers"shippingprice"中得到错误错误显示:

AttributeError: 'NoneType' object has no attribute 'text'

我可以从"title"中获得文本值,但当我输入"title"时,没有其他值;。文本";在行的末尾或打印功能中。脚本在运行时会提取正确的信息,但我不想要html标记。

results = soup.find_all("div", {"class": "s-item__info clearfix"}) #to separate the section of text for each item on the page
for item in results:
product = {
'title': item.find("h3", attrs={"class": "s-item__title s-item__title--has-tags"}).text,
'date': item.find("div", attrs={"class": "s-item__title--tag"}), #.find("span", attrs={"class": "POSITIVE"}),
'soldprice': item.find("span", attrs={"class": "s-item__price"}),
'shippingprice': item.find("span", attrs={"class": "s-item__shipping s-item__logisticsCost"}),
}
print(product)

问题是因为在报价之前,还有其他div带有class="s-item__info clearfix",但没有date, soldprice,shippingprice

您必须添加find才能仅在优惠中搜索

results = soup.find('div', class_='srp-river-results clearfix').find_all("div", {"class": "s-item__info clearfix"})

最新更新