【问题标题】:HTML parsing line by line逐行解析HTML
【发布时间】:2019-06-24 14:28:36
【问题描述】:

我正在编写一个用于解析 HTML 的 Python 代码。此处的目标是在每一行中查找字符串,并按如下所示进行更改:

原文:《Criar Alerta》

<li><a href="http://..." target="_blank">Criar Alerta</a></li>

预期结果:“创建警报”

<li><a href="http://..." target="_blank">Create alert</a></li>

然后,为了确保创建一个与原始 HTML 结构相同的新 HTML,我需要逐行解析后面的字符串,识别字符串,并将其更改为字典中的等价物。

我看到here,BeautifulSoup 可以解析特定的标签。我试过了,但我不确定结果。

然后我问:是否可以使用 BeautifulSoup 进行逐行解析,因为它适用于标签,并且每行有多个标签?

提前致谢,

蒂亚戈

【问题讨论】:

  • 我看到一个帖子 here 引导我找到我的问题中的 link
  • 我认为你需要更具体一点。你能举几个例子,相关的字典和想要的输出吗?

标签: python python-3.x beautifulsoup


【解决方案1】:

我相信以下是您正在寻找的。

让我们使用 3 行,其中两行包含字典中的单词,另一行不包含 - 只是为了测试代码:

rep = """
      <li class="current"><a  style="color:#00233C;" href="index.html"><i class="icon icon-home"></i>  Início</a></li>
      <li class="current"><a  style="color:#00233C;" href="index.html"><i class="icon icon-home"></i>  Nunca</a></li>
      <li class="current"><a  style="color:#00233C;" href="index.html"><i class="icon icon-home"></i>  Criar Alerta</a></li>
    """

并使用您的字典(提示:将字典定义为 dict 绝不是一个好主意;它只是在路上的某个地方自找麻烦......)

rep_dict = {
"Início": "Start",
"Ajuda": "Help",
"Criar Alerta": "Create Alert",
"Materiais e Estruturas": "Structures and Materials" 
}

现在是代码:

soup = BeautifulSoup(rep, 'lxml')

only_a_tags = soup.find_all('a')

for item in range(len(only_a_tags)):
    for word in rep_dict:
        if word in str(only_a_tags[item]):
            print(str(only_a_tags[item]).replace(word,rep_dict[word]))

输出:

<a href="index.html" style="color:#00233C;"><i class="icon icon-home"></i>  Start</a>
<a href="index.html" style="color:#00233C;"><i class="icon icon-home"></i>  Create    Alert</a>

没有打印包含“nunca”的项目,因为“nunca”不在rep_dict中。

【讨论】:

  • 代码在我的 html 文件中运行良好。我将把字典写在一个特定的 .py 文件中,因为它有 200 多个条目,并且仍然可以更新。您对“dict”名称是正确的。非常感谢您的帮助!
【解决方案2】:

@杰克转瞬

在下面的示例中,我想将“Início”替换为“Start”:

原文:

<li class="current"><a  style="color:#00233C;" href="index.html"><i class="icon icon-home"></i>  Início</a></li>

预期结果:

<li class="current"><a  style="color:#00233C;" href="index.html"><i class="icon icon-home"></i>  Start</a></li>

字典中的一个例子:

dict = {
    "Início": "Start",
    "Ajuda": "Help",
    "Criar Alerta": "Create Alert",
    "Materiais e Estruturas": "Structures and Materials"
    ...
}

以下是我编写的代码,用于练习使用 BeautifulSoup 进行 HTML 解析。 (我注意到所有要替换的字符串都在“a”标签内,然后我使用了SoupStrainer(“a”))

from bs4 import BeautifulSoup
from bs4 import SoupStrainer

with open(html_file, 'rb') as src:
    doc = src.read()
    src.close()

only_a_tags = SoupStrainer("a")
parse_1 = 'html.parser'
soup = BeautifulSoup(doc, parse_1, parse_only=only_a_tags)

print(soup.prettify())

原行解析打印如下:

<a href="index.html" style="color:#00233C;">
 <i class="icon icon-home">
 </i>
 Início
</a>

鉴于上面的打印,我不确定是否能够获得预期的结果。

我的目的是找到每一行的字符串,然后在字典中搜索它的等价物,然后执行替换。

现在,我想知道如何使用 BeatifulSoup 执行这种字符串替换。 之后,我将编写一个“for”循环,最终对 HTML 文件中的所有行进行替换。

我的第一次尝试(在了解 BeautifulSoup 之前)是处理以二进制形式读取的 HTML 文件的 .txt 版本,这证明自己非常耗时且效率低下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    相关资源
    最近更新 更多