python - BeautifulSoup 返回 noneType
问题描述
我正在尝试使用以下 Python 代码从 EDGAR 数据库中获取一些数据。
html1 = 'https://www.sec.gov/Archives/edgar/data/320193/000032019317000070/aapl-20170930.xml'
xbrl_resp = requests.get(html1)
xbrl_str = xbrl_resp.text
soup1 = BeautifulSoup(xbrl_str, 'lxml')
mytag = soup1.find('us-gaap:StockholdersEquity',{'contextRef':'FI2017Q4'})
print(mytag)
即使标记存在于 xml 文件中,它也不会返回任何内容。任何建议,将不胜感激
解决方案
您遇到了几个问题。首先,通过请求的内容而不是文本。其次,使用 xml 解析器而不是 lxml 解析器。最后,您在“us-gaap:StockholdersEquity”标签中搜索错误。
html1 = 'https://www.sec.gov/Archives/edgar/data/320193/000032019317000070/aapl-20170930.xml'
xbrl_resp = requests.get(html1)
xbrl_str = xbrl_resp.content
soup1 = BeautifulSoup(xbrl_str, 'xml')
mytag = soup1.find('us-gaap:StockholdersEquity',contextRef='FI2017Q4')
print(mytag)
推荐阅读
- spring-retry - 如何使用 Spring Retry 在用尽重试时包装异常
- gitlab - 如何在 gitlab-ci 运行期间修复“没有这样的文件或目录”
- api - 当我使用 https 地址时,为什么 axios 在 http 中调用我的 api
- c# - 如何从 Xamarin 移动应用程序以编程方式生成 PDF 文件
- sql - 如何选择不同的行但最大日期?
- ios - “添加到主屏幕”图标在带有 HTTPS 的 iOS 13 上不起作用
- java - 如何在java中获取一个人的年月和日数的出生日期
- linux - 如何在多个文件中搜索字符串?如果不存在则显示文件名
- apache-spark - Databricks 结果缓存
- php - Mysql join 三表/ Laravel 查询