试图构建美丽集团,以灵活处理公司年度报告

2024-10-02 06:32:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图使用美国证券交易委员会(SEC)的数据库,查看公司财务报告(称为10k),为每次提交拿出一份执行委员会成员名单。我目前正在使用微软(股票代码:MSFT)和沃尔玛(股票代码:WMT)的最新文件。我知道我可以在其他金融网站上查找这些信息,但我正在尝试建立一个灵活的数据库供个人使用。我的问题:

  1. 每个报告中的表索引位置不同,在一个公司报告中,我想要的表可能会不同 在另一个表上,它可能是表38,因此静态索引/位置计数不会在整个表中起作用 多个文件
  2. 每个HTML表标记中的特定属性都会更改,因此我无法搜索公共属性。在里面 在某些情况下,我找到了共同的属性,但有时我找不到

我开始认为我可能无法自动执行此操作,因为缺少在每个文件中唯一且在所有文件中通用的标识符。在过去的几周里,我看了很多Python网页垃圾教程和视频,真是头痛不已。任何建议都会得到赞赏,完全自动化将是理想的,这样我就可以循环通过多个文件,部分帮助我在这里学习。我可能会碰到尝试自动化一些太多样化的东西

微软链接:

https://www.sec.gov/Archives/edgar/data/789019/000156459019027952/msft-10k_20190630.htm

所需表格:

<table border="0" cellspacing="0" cellpadding="0" align="center" style="border-collapse:collapse; width:100%;">

沃尔玛链接:

https://www.sec.gov/Archives/edgar/data/104169/000010416919000016/wmtform10-kx1312019.htm

所需表格:

<table cellpadding="0" cellspacing="0" style="font-family:Times New Roman;font-size:10pt;width:100%;border-collapse:collapse;text-align:left;">

计算每页表数的代码:

from selenium import webdriver
from bs4 import BeautifulSoup

chrome_path = r"C:\webdrivers\chromedriver.exe"
browser = webdriver.Chrome(chrome_path)

#Microsoft
browser.get("https://www.sec.gov/Archives/edgar/data/789019/000156459019027952/msft-10k_20190630.htm")
msft = browser.page_source
page_msft = BeautifulSoup(msft, 'html.parser')
tables_msft = page_msft.find_all("table")

#Walmart
browser.get("https://www.sec.gov/Archives/edgar/data/104169/000010416919000016/wmtform10-kx1312019.htm")
wmt = browser.page_source
page_wmt = BeautifulSoup(wmt, 'html.parser')
tables_wmt = page_wmt.find_all("table")

print("MSFT Result Table Count: " + str(len(tables_msft)))
print("Walmart Result Table Count: " + str(len(tables_wmt)))

结果:

MSFT结果表计数:263

沃尔玛结果表计数:258

进程已完成,退出代码为0


Tags: 文件httpsbrowserdatawwwpagetablesec
1条回答
网友
1楼 · 发布于 2024-10-02 06:32:14

首先,您不需要Selenium,请求库将更快并避免开销。因此,我能够部分地找到提取所需数据的方法。但是由于列数不同,它们不能组合在一起(对于微软和沃尔玛)。 下面的代码生成两个必需的数据帧,一个用于Microsoft,一个用于Walmart。 您仍然需要操纵列名。这样做的目的是获取td值为“Age”的表,因为它是唯一的表数据。如果您需要澄清,请告诉我:-

from bs4 import BeautifulSoup
import requests
import pandas as pd
import numpy as np


#Microsoft
page = requests.get("https://www.sec.gov/Archives/edgar/data/789019/000156459019027952/msft-10k_20190630.htm")
soup = BeautifulSoup(page.text, 'html')
resmsft = []
tables_msft = soup.find(text="Age").find_parent("table")
for row in tables_msft.find_all("tr")[1:]:
#    print([cell.get_text(strip=True) for cell in row.find_all("td")])
    if row:
        resmsft.append([cell.get_text(strip=True) for cell in row.find_all("td")])

non_empty = [sublist for sublist in resmsft if any(sublist)]
df_msft = pd.DataFrame.from_records(non_empty)
df_msft[df_msft==''] = np.nan 
df_msft=df_msft.dropna(axis=1,how='all')


#Walmart
page = requests.get("https://www.sec.gov/Archives/edgar/data/104169/000010416919000016/wmtform10-kx1312019.htm")
soup = BeautifulSoup(page.text, 'html')
#page_wmt = BeautifulSoup(soup, 'html.parser')
tables_wmt = soup.find(text="Age").find_parent("table")
reswmt = []
for row in tables_wmt.find_all("tr")[1:]:
#    print([cell.get_text(strip=True) for cell in row.find_all("td")])
    if row:
        reswmt.append([cell.get_text(strip=True) for cell in row.find_all("td")])
non_empty_wmt = [sublist for sublist in reswmt if any(sublist)]
df_wmt = pd.DataFrame.from_records(non_empty_wmt)
df_wmt[df_wmt==''] = np.nan 
df_wmt=df_wmt.dropna(axis=1,how='all')

相关问题 更多 >

    热门问题