我使用beautifulsoup删除了一些标记,从而修改了一个html文件。现在我想把结果写回一个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).
我想得到像print i这样的结果:
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.
如何获得与print i相同的结果(即标记及其内容显示在同一行)?谢谢。