Python BeautifulSoup在找到的关键字周围添加标记

2024-10-01 11:23:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在做一个项目,在这个项目中,我想允许regex在一个巨大的HTML文件集上搜索。在

在第一次查明我感兴趣的文件后,我现在想突出显示找到的关键字!在

使用BeautifulSoup我可以确定我的关键字所在的节点。我要做的一件事就是改变整个父母的颜色。在

不过,我还想在找到的关键字周围添加我自己的<;span>;-标记。在

使用BFSoup提供的find()-函数来确定位置等并不是什么大事。但是在普通文本周围添加我的标签似乎是不可能的?在

# match = keyword found by another regex
# node = the node I found using the soup.find(text=myRE)
node.parent.setString(node.replace(match, "<myspan>"+match+"</myspan>"))

这样,我只添加文本而不是适当的标记,因为文档不是刚解析的,我希望避免这种情况!在

我希望我的问题变得清楚一点:)


Tags: 文件the项目标记文本nodehtmlmatch
2条回答

如果你加上文字。。。在

my_tag = node.parent.setString(node.replace(match, "<myspan>"+match+"</myspan>"))

…再把它传给美丽集团

^{pr2}$

它应该被分类为一个BS-tag对象,并可用于解析。在

您可以将这些更改应用于原始大量文本,并将其作为一个整体来运行,以避免重复。在

编辑:

docs

# Here is a more complex example that replaces one tag with another: 

from BeautifulSoup import BeautifulSoup, Tag
soup = BeautifulSoup("<b>Argh!<a>Foo</a></b><i>Blah!</i>")
tag = Tag(soup, "newTag", [("id", 1)])
tag.insert(0, "Hooray!")
soup.a.replaceWith(tag)
print soup
# <b>Argh!<newTag id="1">Hooray!</newTag></b><i>Blah!</i>

下面是一个简单的示例,展示了一种方法:

import re
from bs4 import BeautifulSoup as Soup

html = '''
<html><body><p>This is a paragraph</p></body></html>
'''

(1)存储文本并清空标签

^{pr2}$

(2)找出要加粗的单词的起始位置和结束位置(为我的英语道歉)

match = re.search(r'\ba\b', text)
start, end = match.start(), match.end()

(3)拆分文本,增加第一部分

soup.p.append(text[:start])
print soup

(4)创建一个标记,向其添加相关文本,并将其附加到父对象

b = soup.new_tag('b')
b.append(text[start:end])
soup.p.append(b)
print soup

(5)附加正文其余部分

soup.p.append(text[end:])
print soup

下面是上面的输出:

<html><body><p></p></body></html>
<html><body><p>This is </p></body></html>
<html><body><p>This is <b>a</b></p></body></html>
<html><body><p>This is <b>a</b> paragraph</p></body></html>

相关问题 更多 >