首页 > 解决方案 > 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 文件中,它也不会返回任何内容。任何建议,将不胜感激

标签: pythonxmlxbrl

解决方案


您遇到了几个问题。首先,通过请求的内容而不是文本。其次,使用 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)

推荐阅读