python - 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。任何帮助,将不胜感激。
解决方案
您的问题是,soup3.find_all() 返回结果列表,并且您尝试在此列表上使用 .get() 方法,而您应该只在一个项目上使用它。
尝试像遍历它们并打印每一个:
pressting = soup3.find_all("a", string="UPS")
for i in pressting:
print(i.get('href'))
推荐阅读
- javascript - 在我的 chrome 扩展中从内容发送数据以响应应用程序
- go - 将结构字段编组为没有字段名称的 JSON
- neo4j - 在 Neo4j 中以字符串开头的列表中搜索
- oauth-2.0 - OAuth2 和 OpenID API 到 API 的通信
- django - 事件列表上的 Django 查询:查找最近的事件
- excel - 使用 sum if 根据出击类型计算总飞行小时数
- c++ - C++ 模板只接受具有特定成员的类型名
- javascript - Javascript 将字符串转换为对象(列表/数组)
- laravel - Laravel 旋转 Eloquent 模型
- google-apps-script - Google 应用脚本 - UrlFetchApp.Fetch 无法发布