如何使用不完整的HTML代码进行换行?
content="<head></head><a href="http://example.com/">I linked to <i>example.com</i></a><p>#1</p><p>#2</p>"
soup = bs4.Beautifulsoup(content, 'html.parser')
tag = soup.new_tag('html')
tag.wrap(soup)
我试过循环,但标记了错误。
tag = soup.new_tag('html')
for item in soup.find_all():
tag.append(item)
答案 0 :(得分:0)
from bs4 import BeautifulSoup
content="""<head></head><a href="http://example.com/">I linked to <i>example.com</i></a><p>#1</p><p>#2</p>"""
soup = BeautifulSoup(content, 'html.parser')
tag = soup.new_tag('html')
for item in soup.find_all():
tag.append(item)
print(tag)
<html><head></head><a href="http://example.com/">I linked to </a><i>example.com</i><p>#1</p><p>#2</p></html>
I linked to 的 example.com 的
#1
#2