【问题标题】:Regex/Beautifulsoup HTML parsing正则表达式/Beautifulsoup HTML 解析
【发布时间】:2021-07-20 06:55:49
【问题描述】:

上下文:我有一个大型 HTML 文档,其中包含我要提取的业务数据。我选择使用正则表达式,但如果用户想要提供 BS 逻辑来解决问题,我会打开 Beautifulsoup。下面是文档的sn-p。该文档包含一系列重复的 HTML 部分,其模式如所示。粗体字是我要提取的正则表达式模式目标。下面也是我开始尝试提取交易描述的 Python 脚本的 sn-p,这是 sn-p 中的第一个字段 (ISSUEMO)

第一个函数的作用是扫描文档以获取交易描述并打印每个交易的索引位置。

字符串匹配“ISSUEMO”在 15102:15109

我想在第二个函数中做的是提取并打印始终遵循函数一中交易描述的交易 ID (1MOI-00237)

HTML 片段

<tr class="style_12" valign="top" align="left">
                                            <td style=" overflow:hidden; border-bottom: 1px solid rgb(222, 219, 239);">
                                                    <div class="style_51" style=" text-align:left;">ISSUEMO</div>
                                                </td>
                                            <td style=" overflow:hidden; border-bottom: 1px solid rgb(222, 219, 239);">
                                                    <div class="style_51" style=" text-align:left;">1MOI-00237</div>
                                            ...
                                            ...
                                            ...

                                            <td style=" overflow:hidden; border-bottom: 1px solid rgb(222, 219, 239);">
                                                    <div class="style_97" style=" text-align:right;">12.86</div>
                                                </td>
                                            <td style=" overflow:hidden; border-bottom: 1px solid rgb(222, 219, 239);">
                                                    <div class="style_98" style=" text-align:right;">-64.30</div>
                                                </td>
                                            </tr>

Python

def find_transaction_desc():

    regex_pattern = re.compile(r'ADJQTY|ADJCST|ISSUEPAO|TRNFLOC|RCPTMISC|ISSUEPO|TRNFPAO|RESVN|ISSUEMO|RCPTMO|ADJSCRAP|TRNFRCPT|TRNFINSP|PO|RETVEND|TRNFMRB|PHYSCNT|REQ|SO|MO|APLYPOINFO|GENPO|STDCSTTVAR')

    for match in re.finditer(regex_pattern, html_doc):
        start = match.start()
        end = match.end()
        print('String match "%s" at %d:%d' % (html_doc[start:end], start, end))

find_transaction_desc()

#def extract_transaction_ids():

#extract_transaction_ids()

问题:我不是 python 专家。有人可以提供指针或新模式来解决捕获和打印 ID 或 BS 逻辑的问题吗?

【问题讨论】:

  • 使用美丽的汤;你不会后悔或召唤ZA̡͊͠͝LGΌ
  • 如果你想找到的都在一个标签内,你可能有机会使用正则表达式,但是......它不是。去BS。
  • 对于建议我使用 BS 的人,您能否提供一些示例代码来解决问题?
  • 这里有很多关于使用 BS 抓取的问题 - 只需搜索,错误,BeautifulSoup 也许?

标签: python html regex parsing


【解决方案1】:

如果我对您的理解正确,这就是使用 beautifulsoup 的方法,至少使用您问题中的示例 html(这些可能适用于您的实际文件,也可能不适用于您的实际文件):

from bs4 import BeautifulSoup as bs
soup=bs(html_doc,'html.parser')
for item in soup.select('td'):
    if 'ISSUEMO' in item.text:
        target = item.findNextSibling('td')
        print(target.text.strip())

实际上使用 lxml 和 xpath 更容易:

import lxml.html as lh
doc = lh.fromstring(html_doc)
target = doc.xpath('//td["ISSUEMO"]//following-sibling::td')
print(target[0].text_content().strip())

target = doc.xpath('//td["ISSUEMO"]//following-sibling::td/div')
print(target[0].text.strip())

在这两种情况下,输出都是

1MOI-00237

【讨论】:

    猜你喜欢
    • 2016-05-01
    • 2010-09-08
    • 1970-01-01
    • 2014-12-06
    • 2012-09-12
    • 2014-05-16
    • 1970-01-01
    • 2014-06-08
    相关资源
    最近更新 更多