我通过使用beautifulsoup删除一些标记来修改html文件,然后我想将结果写回html文件。 我的代码:python BeautifulSoup如何将输出写入html文件
from bs4 import BeautifulSoup
from bs4 import Comment
soup = BeautifulSoup(open('1.html'),"html.parser")
[x.extract() for x in soup.find_all('script')]
[x.extract() for x in soup.find_all('style')]
[x.extract() for x in soup.find_all('meta')]
[x.extract() for x in soup.find_all('noscript')]
[x.extract() for x in soup.find_all(text=lambda text:isinstance(text, Comment))]
html =soup.contents
for i in html:
print i
html = soup.prettify("utf-8")
with open("output1.html", "wb") as file:
file.write(html)
但因为我使用soup.prettify,它生成的HTML这样
BATAM.TRIBUNNEWS.COM, BINTAN
- Tradisi pedang pora mewarnai serah terima jabatan pejabat di
Polres
Bintan
, Senin (3/10/2016).
但我要得到这样的打印我做的结果。像这样:
BATAM.TRIBUNNEWS.COM, BINTAN - Tradisi pedang pora mewarnai serah terima jabatan pejabat di Polres Bintan, Senin (3/10/2016).
Empat perwira baru Senin itu diminta cepat bekerja. Tumpukan pekerjaan rumah sudah menanti di meja masing masing.
所以如何使结果与打印i完全一样。所以标签和它的内容将在同一行。由于