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


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>
""")

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

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)

最新更新