如何在python中使用美丽的汤添加li标签

时间:2017-06-21 06:38:14

标签: html python-2.7 beautifulsoup html-lists

from bs4 import BeautifulSoup 
soup = BeautifulSoup("""
<html>
    <body>
        <ul>
            <li>tweetDisplayName</li>
            </br>
            <li>Link:<a href="tweetLink">tweetLink</a></li>
            </br>
        </ul>
        </p>
    </body>
</html>
""")

我需要在li中使用tweetDisplayName在python中添加BeautifulSoup标记。有人知道吗?

1 个答案:

答案 0 :(得分:0)

li_new = soup.new_tag("li")
li_new['class'] = 'cls'
li_new.append("content")
li_element = soup.find_all('li', text='tweetDisplayName')[0]
li_element.insert_after(li_new)