首页 > 解决方案 > ResultSet 对象没有属性“get”

问题描述

嗨,我目前正在尝试使用 beautifulsoup 抓取此https://www.sec.gov/ix?doc=/Archives/edgar/data/1090727/000109072720000003/form8-kq42019earningsr.htm SEC 链接以获取包含“UPS”的链接"

pressting = soup3.find_all("a", string="UPS")
linkkm = pressting.get('href')
print(linkkm)

但是当我这样做时,我得到了这个错误:

Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\SEC.py", line 55, in <module>
    print('Price: ' + str(edgar()))
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\SEC.py", line 46, in edgar
    linkkm = pressting.get('href')
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\bs4\element.py", line 2081, 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 'get'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

我的预期结果是提取href,然后打印该href。任何帮助,将不胜感激。

标签: pythonweb-scrapingbeautifulsoup

解决方案


您的问题是,soup3.find_all() 返回结果列表,并且您尝试在此列表上使用 .get() 方法,而您应该只在一个项目上使用它。

尝试像遍历它们并打印每一个:

pressting = soup3.find_all("a", string="UPS")
for i in pressting:
    print(i.get('href'))

推荐阅读