python - 不能使用美丽的汤只打印文本
问题描述
我正在努力在 python3 上创建我的第一个项目。当我使用以下代码时:
def scrape_offers():
r = requests.get("https://www.olx.bg/elektronika/kompyutrni-aksesoari-chasti/aksesoari-chasti/q-1070/?search%5Border%5D=filter_float_price%3Aasc", cookies=all_cookies)
soup = BeautifulSoup(r.text,"html.parser")
offers = soup.find_all("div",{'class':'offer-wrapper'})
for offer in offers:
offer_name = offer.findChildren("a", {'class':'marginright5 link linkWithHash detailsLink'})
print(offer_name.text.strip())
我收到以下错误:
Traceback (most recent call last):
File "scrape_products.py", line 45, in <module>
scrape_offers()
File "scrape_products.py", line 40, in scrape_offers
print(offer_name.text.strip())
File "/usr/local/lib/python3.7/site-packages/bs4/element.py", line 2128, in __getattr__
"ResultSet object has no attribute '%s'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute 'text'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
我在 StackOverFlow 上读过很多类似的案例,但我还是忍不住。如果有人有任何想法,请帮助:)
PS:如果我在没有.text
它的情况下运行代码,则会显示整个<a class=...> ... </a>
解决方案
findchildren 返回一个列表。有时你得到一个空列表,有时你得到一个包含一个元素的列表。
您应该添加一个 if 语句来检查返回列表的长度是否大于 1,然后打印文本。
import requests
from bs4 import BeautifulSoup
def scrape_offers():
r = requests.get("https://www.olx.bg/elektronika/kompyutrni-aksesoari-chasti/aksesoari-chasti/q-1070/?search%5Border%5D=filter_float_price%3Aasc")
soup = BeautifulSoup(r.text,"html.parser")
offers = soup.find_all("div",{'class':'offer-wrapper'})
for offer in offers:
offer_name = offer.findChildren("a", {'class':'marginright5 link linkWithHash detailsLink'})
if (len(offer_name) >= 1):
print(offer_name[0].text.strip())
scrape_offers()
推荐阅读
- android - 我的应用程序不符合使用请求的权限 Google 新政策的条件
- c - 没有 -fPIC 的全局变量的位置
- integer - 整数的反转数字——相似的代码不同的输出
- logstash - 在 Logstash 中转换多维数组
- javascript - Angular 7 使用 *ngfor 对数据进行分组
- linux - 如果第一个单元格的值满足条件,如何从 CSV 中删除一行?
- python - Python监控radiusd服务。负载均衡器 tcp 健康检查
- c++ - 公式与指定值不匹配的 C++ 结果
- javascript - 使用模板替换 https 链接
- javascript - 使用 jQuery 更改滑块的值