soup = BeautifulSoup(html,'html.parser')
text = soup.text # Only the text from the soup
soup.body.clear() #Clear the text between the body tags
new_text = text.split() # Split beacuse of the spaces much easier
for i in new_text:
new_tag = soup.new_tag('span') #Create a new tag
new_tag.append(i) #Append i to it (from the list that's split between spaces)
#example new_tag('a') when we append 'word' to it it will look like <a>word</a>
soup.body.append(new_tag) #Append the whole tag e.g. <span>one-word</span)
我做过这样的事情,变量html是您的代码
<html><body>word-one word-two word-one</body></html>
,我将文本和代码分开,然后将它们添加到一起。在我们也可以用正则表达式来匹配某个单词。在
^{pr2}$我相信我们可以用类似的方式使用
.insert
。在相关问题 更多 >
编程相关推荐