【问题标题】:Extracting text section from (Edgar 10-K filings) HTML从(Edgar 10-K 文件)HTML 中提取文本部分
【发布时间】:2019-12-25 16:07:53
【问题描述】:

我正在尝试从 HTML 文件中提取某个部分。具体来说,我查找 10-K 文件(某公司的美国业务报告)的“ITEM 1”部分。例如。: https://www.sec.gov/Archives/edgar/data/1591890/000149315218003887/form10-k.htm#a_002

问题:但是,我找不到“ITEM 1”部分,也不知道如何告诉我的算法从“ITEM 1”那个点搜索到另一个点(例如“ITEM 1A”)并提取其间的文本。

非常感谢您的帮助。

除其他外,我已经尝试过这个(和类似的),但我的bd 总是空的:

    try:
        # bd = soup.body.findAll(text=re.compile('^ITEM 1$'))
        # bd = soup.find_all(name="ITEM 1")
        # bd = soup.find_all(["ITEM 1", "ITEM1", "Item 1", "Item1", "item 1", "item1"])

        print(" Business Section (Item 1): ", bd.content)

    except:
        print("\n Section not found!")

使用 Python 3.7 和 Beautifulsoup4

问候赫卡

【问题讨论】:

  • 我相信用xpath更容易,也就是说不用beautifulsoup,而是用lxml。如果您有兴趣,我可以发布答案。
  • 感谢您的回答。如果你能给我一个关于你的 lxml 解决方案的提示,那就太好了。我之前也尝试过,但无法管理。
  • 我不确定您需要什么样的提示。我可以按照我的建议发布答案,您可以对其进行测试。答案适用于该特定文件,但所有 EDGAR 文件的根本问题是它们不需要使用统一格式,因此每个文件管理器/edgarization 提供者对它们的格式不同,这意味着许多解决方案有时有效,有时无效。这只是 EDGAR 的现实生活......
  • 啊,我明白了!谢谢。我很乐意尝试您的解决方案!
  • 见下面的答案。

标签: python html beautifulsoup text-extraction edgar


【解决方案1】:

正如我在评论中提到的,由于 EDGAR 的性质,这可能对一个文件有效,但对另一个文件无效。不过,这些原则通常应该有效(经过一些调整......)

import requests
import lxml.html

url = 'https://www.sec.gov/Archives/edgar/data/1591890/000149315218003887/form10-k.htm#a_002'
source = requests.get(url)
doc = lxml.html.fromstring(source.text)

tabs = doc.xpath('//table[./tr/td/font/a[@name="a_002"]]/following-sibling::p/font')
#in this filing, Item 1 is hiding in a series of <p> tags following a table with an <a> tag with a 
#"name" attribute which has a value of "a_002"
flag = ''
for i in tabs:
    if flag == 'stop':
        break
    if i.text is not None: #we now start extracting the text from each <p> tag and move to the next
        print(i.text_content().strip().replace('\n',''))
    nxt = i.getparent().getnext()
    #the following detects when the <p> tags of Item 1 end and the next Item begins and then stops 
    if str(type(nxt)) != "<class 'NoneType'>" and nxt.tag == 'table':
        for j in nxt.iterdescendants():
           if j.tag == 'a' and j.values()[0]=='a_003':
                 # we have encountered the <a> tag with a "name" attribute which has a value of "a_003", indicated the beginning of the next Item; so we stop
                 flag='stop'           

输出是本文件中第 1 项的文本。

【讨论】:

    【解决方案2】:

    有特殊字符。先删除它们

    import requests
    from simplified_scrapy.simplified_doc import SimplifiedDoc 
    html = requests.get('https://www.sec.gov/Archives/edgar/data/1591890/000149315218003887/form10-k.htm#a_002').text
    doc = SimplifiedDoc(html)
    doc.loadHtml(doc.replaceReg(doc.html, 'ITEM[\s]+','ITEM '))
    item1 = doc.getElementByText('ITEM 1')
    print(item1) # {'tag': 'B', 'html': 'ITEM 1. BUSINESS'}
    
    # Here's what you might use
    table = item1.getParent('TABLE')
    trs = table.TRs
    for tr in trs:
      print (tr.TDs)
    

    如果你使用的是最新版本,可以使用以下方法

    import requests
    from simplified_scrapy.simplified_doc import SimplifiedDoc 
    html = requests.get('https://www.sec.gov/Archives/edgar/data/1591890/000149315218003887/form10-k.htm#a_002').text
    doc = SimplifiedDoc(html)
    item1 = doc.getElementByReg('ITEM[\s]+1') # Incoming regex
    print(item1,item1.text) # {'tag': 'B', 'html': 'ITEM\n    1. BUSINESS'} ITEM 1. BUSINESS
    
    # Here's what you might use
    table = item1.getParent('TABLE')
    trs = table.TRs
    for tr in trs:
      print (tr.TDs)
    

    【讨论】:

    • 您好,谢谢您的回复。我试过了,但item1 始终是None。我认为doc.getElementByText('ITEM 1')找不到文本,即使我将其替换为doc.loadHtml(doc.replaceReg(doc.html, 'ITEM[^\S]+1','ITEM ')),如果我正确理解代码。
    • 谢谢,我又检查了一遍,但我仍然得到Noneìtem1
    • 我有版本:0.8.91。我也做过pip install --upgrade simplified_scrapy,但它已经是最新的了!
    • 对不起,我帮不了你。我这里没有问题。我不知道怎么了。
    • 不用担心,无论如何,您的代码有所帮助和澄清,这意味着我仍然学到了一些东西。谢谢!
    猜你喜欢
    • 2019-04-10
    • 2022-07-25
    • 2019-12-10
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多