我目前正在做一个项目,在这个项目中,我想允许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>"))
这样,我只添加文本而不是适当的标记,因为文档不是刚解析的,我希望避免这种情况!在
我希望我的问题变得清楚一点:)
如果你加上文字。。。在
…再把它传给美丽集团
^{pr2}$它应该被分类为一个BS-tag对象,并可用于解析。在
您可以将这些更改应用于原始大量文本,并将其作为一个整体来运行,以避免重复。在
编辑:
从docs:
下面是一个简单的示例,展示了一种方法:
(1)存储文本并清空标签
^{pr2}$(2)找出要加粗的单词的起始位置和结束位置(为我的英语道歉)
(3)拆分文本,增加第一部分
(4)创建一个标记,向其添加相关文本,并将其附加到父对象
(5)附加正文其余部分
下面是上面的输出:
相关问题 更多 >
编程相关推荐