【问题标题】:Using lxml.html to parse large html document使用 lxml.html 解析大型 html 文档
【发布时间】:2019-01-09 14:50:01
【问题描述】:
<td style="vertical-align:bottom;background-color:#efefef;padding-left:2px;padding-top:2px;padding-bottom:2px;padding-right:2px;">
  <div style="text-indent:26px;font-size:9pt;">
    <font style="font-family:Helvetica,sans-serif;font-size:9pt;">
    iPhone
    </font>
    <font style="font-family:Helvetica,sans-serif;font-size:9pt;">
    <sup style="vertical-align:top;line-height:120%;font-size:pt">
    (1)
    </sup>
    </font>
  </div>
</td>
<td style="vertical-align:bottom;padding-left:2px;padding-top:2px;padding-bottom:2px;background-color:#efefef;">
  <div style="text-align:left;font-size:9pt;">
    <font style="font-family:Helvetica,sans-serif;font-size:9pt;">
    $
    </font>
  </div>
</td>
<td style="vertical-align:bottom;background-color:#efefef;padding-top:2px;padding-bottom:2px;">
  <div style="text-align:right;font-size:9pt;">
    <font style="font-family:Helvetica,sans-serif;font-size:9pt;">
    29,906
    </font>
  </div>
</td>
<td style="vertical-align:bottom;background-color:#efefef;">
  <div style="text-align:left;font-size:10pt;">
    <font style="font-family:inherit;font-size:10pt;">
    <br/>
    </font>
  </div>
</td>

我正在尝试使用 lxml 来获取两个字段:iPhone 和 29,906。

这是一个更大的 html 文件的一部分。

我找到了如何提取每个 td 中的字体,但我需要能够匹配 iPhone 字段和 29,906 字段。

我能想到的一种方法是将所有内容放入一个非常长的数组中并搜索“iPhone”并返回 iPhone + 2 值,但这似乎非常冗长且效率低下。

谁能指导我正确的方向?

这是我目前所拥有的:

from bs4 import BeautifulSoup
import requests
from lxml import html, cssselect

link =    "https://www.sec.gov/Archives/edgar/data/320193/000032019318000100/a10-qq320186302018.htm"
response = requests.get(link)
soup = BeautifulSoup(response.text, 'html.parser')
str_soup = str(soup)
doc = html.document_fromstring(str_soup)
for col in doc.cssselect('font'):
    try:
        style = col.attrib['style']
        if style=="font-family:Helvetica,sans-serif;font-size:9pt;":
            print(col.text.strip())
    except:
        pass

这会返回所有文本,但不是我需要的。

【问题讨论】:

    标签: python html parsing beautifulsoup lxml


    【解决方案1】:

    这个怎么样?

    from bs4 import BeautifulSoup
    import re
    
    soup = BeautifulSoup(html, 'html.parser')
    
    x = soup.find_all('font')
    name = re.sub(r"[\n\t\s]*", "", x[0].get_text())
    value = re.sub(r"[\n\t\s]*", "", x[3].get_text())
    
    print(name, 'costs', value)
    

    输出:

    iPhone costs 29,906
    

    【讨论】:

    • 我认为如果文档正是我发布的内容,这将起作用,但我正在查看的文档要大得多,因此很难找到我需要的每个索引值。我会一直知道我需要“iPhone”字段,只是后面的数字会不一样。
    【解决方案2】:

    我没有得到我想要的东西,但这是我迄今为止能想出的东西

    from bs4 import BeautifulSoup
    import requests
    from lxml import html, cssselect
    import csv
    
    
    link = "https://www.sec.gov/Archives/edgar/data/320193/000032019318000100/a10-qq320186302018.htm"
    response = requests.get(link)
    soup = BeautifulSoup(response.text, 'html.parser')
    str_soup = str(soup)
    doc = html.document_fromstring(str_soup)
    
    
    with open('AAPL_financials.csv', 'w') as csvfile:
        writer = csv.writer(csvfile)
        for col in doc.cssselect('tr'):
            row = []
            for text in col.cssselect('font'):
                if text.text == None:
                    continue
                value = text.text.strip()
                if value == "":
                    continue
                if value == "$":
                    continue
                if value == "%":
                    continue
                if value == ")":
                    continue
                if value[0] == "(":
                    value = value.replace("(", "-"))
                row.append(value)
            writer.writerow(row)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      • 1970-01-01
      • 2014-03-28
      • 2015-12-10
      • 2011-07-07
      相关资源
      最近更新 更多