Python 逐行解析HTML
我正在编写一个用于解析HTML的python代码。这里的目标是在每行中查找字符串,并按如下所示对其进行更改: 原文:“Criar警报”Python 逐行解析HTML,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,我正在编写一个用于解析HTML的python代码。这里的目标是在每行中查找字符串,并按如下所示对其进行更改: 原文:“Criar警报” 预期结果:“创建警报” 然后,为了确保创建的新HTML与原始HTML具有相同的结构,我需要逐行解析后面的HTML,识别字符串,并将其从字典中更改为等效的HTML 我看到BeautifulSoup可以解析特定的标记。我试过了,但结果不确定 然后我问:如果BeautifulSoup可以处理标记,并且每行有多个标记,那么是否可以使用BeautifulSoup进
预期结果:“创建警报”
然后,为了确保创建的新HTML与原始HTML具有相同的结构,我需要逐行解析后面的HTML,识别字符串,并将其从字典中更改为等效的HTML
我看到BeautifulSoup可以解析特定的标记。我试过了,但结果不确定
然后我问:如果BeautifulSoup可以处理标记,并且每行有多个标记,那么是否可以使用BeautifulSoup进行逐行解析
提前感谢,
蒂亚戈@Jack Fleeting
在下面的示例中,我想将“Início”替换为“Start”:
原件:
<li class="current"><a style="color:#00233C;" href="index.html"><i class="icon icon-home"></i> Início</a></li>
下面是我编写的代码,用于使用BeautifulSoup练习HTML解析。
(我注意到所有要替换的字符串都在“a”标记内,然后我使用了SoupStrainer(“a”))
原始行的分析和打印如下所示:
<a href="index.html" style="color:#00233C;">
<i class="icon icon-home">
</i>
Início
</a>
鉴于上面的打印,我不确定是否能够获得预期的结果
我的目的是找到每行的字符串,然后在字典中搜索其等价项,并执行替换
现在,我想知道如何使用BeatifulSoup执行字符串替换。
之后,我将编写一个“for”循环,以最终替换HTML文件中的所有行
我的第一次尝试(在了解BeautifulSoup之前)是处理一个以二进制形式读取的HTML文件的.txt版本,这证明了它非常耗时且没有效率。@Jack Fleeting
在下面的示例中,我想将“Início”替换为“Start”:
原件:
<li class="current"><a style="color:#00233C;" href="index.html"><i class="icon icon-home"></i> Início</a></li>
下面是我编写的代码,用于使用BeautifulSoup练习HTML解析。
(我注意到所有要替换的字符串都在“a”标记内,然后我使用了SoupStrainer(“a”))
原始行的分析和打印如下所示:
<a href="index.html" style="color:#00233C;">
<i class="icon icon-home">
</i>
Início
</a>
鉴于上面的打印,我不确定是否能够获得预期的结果
我的目的是找到每行的字符串,然后在字典中搜索其等价项,并执行替换
现在,我想知道如何使用BeatifulSoup执行字符串替换。
之后,我将编写一个“for”循环,以最终替换HTML文件中的所有行
我的第一次尝试(在了解BeautifulSoup之前)是处理一个以二进制形式读取的HTML文件的.txt版本,这证明了它非常耗时且没有效率。我相信下面是您正在寻找的内容 让我们使用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>
"""
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]))
现在来看看代码:
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>
"""
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
我相信以下内容就是您要查找的内容
让我们使用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>
"""
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]))
现在来看看代码:
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>
"""
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”这个名字你说得对。非常感谢你的帮助!代码在我的html文件中运行得非常好。我将在一个特定的.py文件中编写字典,因为它有200多个条目,仍然可以更新。“dict”这个名字你说得对。非常感谢你的帮助!