【问题标题】:Getting None when scraping for operating income from SEC EDGAR document从 SEC EDGAR 文件中获取营业收入时一无所获
【发布时间】:2019-12-30 04:02:59
【问题描述】:

我正在尝试从季度填充中获取最近一个季度的营业收入/亏损。

以绿色突出显示的所需输出:financial statement

这是我要抓取的文档的 URL:https://www.sec.gov/ix?doc=/Archives/edgar/data/320193/000032019319000076/a10-qq320196292019.htm

如果您想直观地查看数据点,请参阅第 I 部分,第 1 项。财务报表,营业收入。

我要获取的图形的 HTML 代码:

<ix:nonfraction id="fact-identifier-125" name="us-gaap:OperatingIncomeLoss" contextref="FD2019Q3QTD" unitref="usd" decimals="-6" scale="6" format="ixt:numdotdecimal" data-original-id="d305292495e1903-wk-Fact-6250FB76089207E7F73CB52756E0D8D0" continued-taxonomy="false" enabled-taxonomy="true" highlight-taxonomy="false" selected-taxonomy="false" hover-taxonomy="false" onclick="Taxonomies.clickEvent(event, this)" onkeyup="Taxonomies.clickEvent(event, this)" onmouseenter="Taxonomies.enterElement(event, this);" onmouseleave="Taxonomies.leaveElement(event, this);" tabindex="18" isadditionalitemsonly="false">11,544</ix:nonfraction>

我用来获取这个数据点 (11,544) 的代码:

from bs4 import BeautifulSoup
import requests

url = 'https://www.sec.gov/ix?doc=/Archives/edgar/data/320193/000032019319000076/a10-qq320196292019.htm'

response = requests.get(url)
content = BeautifulSoup(response.content, 'html.parser')

operatingincomeloss = content.find('ix:nonfraction', attrs={"name": "us-gaap:OperatingIncomeLoss", "contextref":"FD2019Q3QTD"})

print (operatingincomeloss)

我也试过

operatingincomeloss = content.find('ix:nonfraction', attrs={"name": "us-gaap:OperatingIncomeLoss"}

最后,我想遍历所有相关的填充物来提取这个数据点。目前,我只是得到无。当我 CTRl+F 通过内容时,我也找不到 ix:nonfraction 标签。

【问题讨论】:

  • 我还是不明白你想要的输出是什么,你能附上一张照片
  • 刚刚附上图片抱歉。该图以绿色突出显示
  • 在下面查看我的答案

标签: python html web-scraping beautifulsoup


【解决方案1】:

页面是通过JavaScript 加载的,我已经附加了XHR 请求并提取了所需的数据。

import requests
from bs4 import BeautifulSoup

r = requests.get(
    "https://www.sec.gov/Archives/edgar/data/320193/000032019319000076/a10-qq320196292019.htm")

soup = BeautifulSoup(r.text, 'html.parser')

for item in soup.select("#d305292495e1903-wk-Fact-6250FB76089207E7F73CB52756E0D8D0"):
    print(item.text)

输出:

11,544

更新:

import requests
from bs4 import BeautifulSoup

r = requests.get(
    "https://www.sec.gov/Archives/edgar/data/320193/000032019319000076/a10-qq320196292019.htm")

soup = BeautifulSoup(r.text, 'html.parser')

for item in soup.findAll("ix:nonfraction", {'contextref': 'FD2019Q3QTD', 'name': 'us-gaap:OperatingIncomeLoss'}):
    print(item.text)

【讨论】:

  • 这是通过选择特定的 ID 完成的,对吧?我希望能够对我将循环浏览的其他文档重复执行相同的操作。所以我将无法知道具体的ID。这就是为什么我选择按 "name": "us-gaap:OperatingIncomeLoss", "contextref":"FD2019Q3QTD" 过滤的原因,因此它可以在不同的填充物中复制。知道为什么我的不起作用吗?
  • @moron 检查我更新的答案,通过 namecontextref 搜索会得到 2 个数字的输出,因为它存在于相同的属性中。
【解决方案2】:

正如@αԋɱҽԃ αмєяιcαη 所说,该页面是通过JavaScript 加载的。 我已使用xhr 请求此代码。

考虑到您使用的属性,我只采用了name 属性,因为contextref 会针对每个元素进行更改。

如果您想循环访问其他元素,也可以更改 name 属性。

正如你所说的要循环遍历这个标签,我已经打印了下面代码中返回的所有输出。

代码:

import requests
from bs4 import BeautifulSoup
res = requests.get('https://www.sec.gov/Archives/edgar/data/320193/000032019319000076/a10-qq320196292019.htm')
soup = BeautifulSoup(res.text, 'html.parser')
for data in soup.find_all('ix:nonfraction', {'name': 'us-gaap:OperatingIncomeLoss'}):
    print(data.text)

输出:

11,544
12,612
48,305
54,780
7,442
7,496
26,329
26,580
3,687
3,892
14,371
15,044
3,221
3,414
12,142
15,285
1,795
1,765
7,199
7,193
1,155
1,127
4,811
4,980
17,300
17,694
64,852
69,082
11,544
12,612
48,305
54,780

【讨论】:

  • 我可以知道为什么我的回答被否决了吗?这不是一个错误的答案吧?
猜你喜欢
  • 2022-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多