开发者

python : Replacing a HTML element depending on its content

I have an html document, in which some elements contains stuff that I want to hide (like the Chinese government is doing, except that I just want to hide confidential information). For example say I have :

<div>
    <span> bkhiu jknd o so so so  yui iou 789 </span>
    <span>
        bkhiu
        <div> 56 898tr SECRET oij890 </div>
    </span>
</div>

And I want to get all the elements that contain the string SECRET, and just replace their whole content by ### :

<div>
    <span> bkhiu jknd o so so so  yui iou 789 </span>
    <span>
        bkhiu
        <div>###</div>
    </span>
</div>

I have thought of using开发者_Python百科 minidom and re with something like :

xmldoc = minidom.parseString(my_html_string)
# filtering nodes by their content
sensitive_nodes = filter(lambda n: re.search('SECRET', n.nodeValue), 
    xmldoc.getElementsByTagName())
# replacing content
for node in sensitive_nodes:
    node.nodeValue = '###'
# output
my_html_string = xmldoc.toxml()

But first the parsing doesn't even succeeds :

ExpatError: mismatched tag: line 27, column 6

And .getElementsByTagName() needs a tagName parameter ... while I don't care about the tag name and need ALL the nodes (in order to filter by their content). Well basically that code doesn't work at all, but is just to try to explain what I wanna achieve.

Any idea how I could do that easily ? With minidom or something completely different ?


Ok ... I have found a very simple way, using BeautifulSoup :

import re
from BeautifulSoup import BeautifulSoup

soup = BeautifulSoup(my_html)
nodes_to_censor = soup.findAll(text=re.compile('.*SECRET.*'))
for node in nodes_to_censor:
    node.replaceWith('###')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜