【问题标题】:Extracting table of holdings from (Edgar 13-F filings) TXT (pre-2013) with python使用 python 从(Edgar 13-F 文件)TXT(2013 年之前)中提取控股表
【发布时间】:2020-11-22 10:12:14
【问题描述】:

我正在从 EDGAR 上的 13-F 表格中提取一份持股表。 2013 年之前的馆藏以 txt 文件的形式提供(参见example)。 我的目标输出是一个 pd.DataFrame,其形状与 txt 文件中的“Form 13F 信息表”相同(10 列,每行单独一行)。

我曾尝试使用 BeautifulSoup,它将表格转换为标签对象,但我不知道如何将其格式化以进入上述数据框。

这是我的代码尝试:

soup2 = BeautifulSoup(requests.get(filing_url_13f).content, 'lxml')
holdings = soup2.find_all('table')

#This is my attempt to turn the content into a list:
lixt=[]
for x in soup2.find_all(['c','c','c','c','c','c','c','c','c']  ):
    for line in x:
        lixt.append(line)
x=lixt[1]
l=[]
for string in x.strings:
    l.append(repr(string))
el=l[7]

这就是我卡住的地方,因为 el 返回以下内容。我不能用 \n 来拆分它,因为公司名称中经常有 \n (AMERICAN\n EXPRESS CO)。

\ namerican \ n Express Co COM 025816109 112,209 1,952,142共享定义的4 1,952,142 - - \ Namerican \ n Express Co COM 025816109 990,116 17,225,400共享定义的4,5 17,225,400 - - \ Namerican \ n Express Co COM 025816109 48,274 839,832共享-Defined 4,7 839,832 - \ namerican \ n Express Co COM 025816109 111,689 1,943,100共享定义的4,8,11,1,943,100 - - \ Namerican \ n Express Co COM 025816109 459,532 7,994,634共享定义的4,10 7,994,634 - - \ Namerican \n EXPRESS CO COM 025816109 6,912,308 120,255,879 Shared-Defined 4, 11 120,255,879 - -\nAMERICAN\n EXPRESS CO COM 025816109 80,456 1,399,713 Shared-Defined 4, 13 1,399,713 COMLAND MIDANIELS\n 039483102 163,151 5,956,600共享定义的4,5 5,956,600 - - \ n York Mellon \ n Corp Com 064058100 206,66058100 206,66058100 206,661 8,04100 206,6608100 206,66088100 206,661 8,04100 206,661 8,04100 206,660881,300共享定义的48,041,300 - - \ n York Mellon \ n Corp Com 064058100 46,104 1,793,915共享-Defined 2, 4, 11 1,793,915 - -\nBANK OF NEW\n YORK MELLON\n CORP COM 064058100 251,827 9,798,700 Shared-Defined 4, 8, 11 9,798,700 - -\nCOCA COLA CO COM 191216100008,0000- 40 -\n

如果有任何建议,我将不胜感激。

【问题讨论】:

    标签: parsing beautifulsoup python-requests edgar


    【解决方案1】:

    是的,这些旧的 EDGAR 文件很糟糕(并不是说新文件要好得多)。这个特别糟糕,因为较长的输入行被分成单独的行以使它们适合页面。

    因此,以下内容应该可以让您足够接近您想要的:

    import pandas as pd
    from bs4 import BeautifulSoup as bs
    import requests
    
    req = requests.get('https://www.sec.gov/Archives/edgar/data/1067983/000119312512470800/d434976d13fhr.txt')
    
    #next is a helper function to put back those longer entires
    def lst_bunch(l,lenth=4):
        i=0
        while i < len(l):
            if len(l[i])<lenth:
                l[i] += l.pop(i+1)
            i += 1
        for item in l:
            if len(item)<lenth:
                lst_bunch(l,lenth)
        else:
            return l
    
    tabs = req.text.replace('<TABLE>','xxx<TABLE>').split('xxx')
    for tab in tabs[2:]:
        soup = bs(tab,'lxml')
        table = soup.select_one('table')
        lines = table.text.splitlines()
        lst_bunch(lines,30)
        for line in lines:
            print(line.strip())
    
    Output:
    Name of Issuer  Class  CUSIP     (In Thousands)   Amount      Discretion   Managers     Sole      Shared  None
    AMERICAN  EXPRESS CO    COM    025816109      110,999     1,952,142 Shared-Defined 4           1,952,142       -   -
    AMERICAN  EXPRESS CO    COM    025816109      979,436    17,225,400 Shared-Defined 4, 5       17,225,400       -   -
    

    等等

    【讨论】:

    • 杰克,谢谢你,它真的很有帮助!我在其他与处理财务数据相关的查询中看到了您的有用建议。有时间可以看看我的另一个问题吗:stackoverflow.com/questions/64958844/…
    猜你喜欢
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 2022-06-30
    • 2016-03-02
    相关资源
    最近更新 更多