开发者

Python. Replacing text between html tags

I want to write a function that highlights some text. It takes a HTML string as input and returns HTML string with additional html tags.

Example: Input string (need to highlight the word "text"):

<div>
<a href="..." title="text to highlight">Some text to highlight</a>
<a href="..." title="text to highlight">Some other text to highlight</a>
</div>

Output string:

<div>
<a href="..." title="text to highlight">Some <b class="highlight">text</b> to highlight</a>
<a href="..." title="text to highlight">Some other <b class="highlight">text</b> to highlight</a>
</div>

I have found a regexp that matches text only between html tags, but I can't figure out how to surround some part of it with additional tags

highlight_str = u'text'
p = re.compile(r"[^<>]+开发者_运维问答(?=[<])")
    iterator = p.finditer(search_str)
    for match in iterator:
        # code for replacement here ???

Is there any other ideas to do it?


Look at Beautiful Soup.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜