繁体   English   中英

如何从具有非结构化表的文本文档中获取值

[英]How to get a value from a text document that has an unstructured table

我正在尝试从10-K文本文件中获取total assets值。 问题是html格式因一家公司而异。

Apple 10-K为例:总资产在具有balance sheet标题的表中,并且该表的某些行中存在现金,存货等典型术语。 在最后一行中,2015年的资产总计为290,479,2014年的资产总计为231,839。我想获得2015年的资产-> 290,479。 我一直无法找到一种方法

1)查找具有某些特定标题(如资产负债表)和行中单词(现金,...)的相关表

2)在具有total assets一词并属于较大年份的行中获取值(在我们的示例中为2015)。

import re
url = 'https://www.sec.gov/Archives/edgar/data/320193/000119312515356351/d17062d10k.htm'
r = requests.get(url)
soup = BeautifulSoup(r.text, "xml")
for tag in soup.find_all(text=re.compile('Total\sassets')):
            print(tag.findParent('table').findParent('table'))

在此处输入图片说明

使用lxmlhtml.parser而不是xml可以得到

title > CONSOLIDATED BALANCE SHEETS
row > Total assets
column 0 > Total assets
column 1 > 
column 2 > $
column 3 > 290,479
column 4 > 
column 5 > 
column 6 > $
column 7 > 231,839
column 8 > 

使用代码

import requests
from bs4 import BeautifulSoup
import re

url = 'https://www.sec.gov/Archives/edgar/data/320193/000119312515356351/d17062d10k.htm'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')# "lxml")

# get all `b` to find title
all_b = soup.find_all('b')
for item in all_b:
    # check text in every `b`
    title = item.get_text(strip=True)
    if title == 'CONSOLIDATED BALANCE SHEETS':
        print('title >', title)
        # get first `table` after `b`
        table = item.parent.findNext('table')
        # all rows in table
        all_tr = table.find_all('tr')
        for tr in all_tr:
            # all columns in row
            all_td = tr.find_all('td')
            # text in first column
            text = all_td[0].get_text(strip=True)
            if text == 'Total assets':
                print('row >', text)
                for i, td in enumerate(all_td):
                    print('column', i, '>', td.get_text(strip=True))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM