beatifullsoap4 diretiva包装

时间:2018-06-18 20:59:13

标签: python-3.x beautifulsoup

如何使用不完整的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)

1 个答案:

答案 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