如何使用Python BeautifulSoup将输出写入html文件 [英] How to write the output to html file with Python BeautifulSoup
本文介绍了如何使用Python BeautifulSoup将输出写入html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我修改了一个html文件,使用 beautifulsoup
删除了一些标签。现在我想把结果写回html文件。
我的代码:
from bs4导入BeautifulSoup
from bs4 import Comment
汤= BeautifulSoup(open('1.html'),html.parser)
[x.extract()for soup.find_all('script')]
soup.find_all('style')]
[x.extract()for x in soup.find_all('meta')]
[x.extract()在soup.find_all(text = lambda text:isinstance(text,Comment))]
html = soup。内容
for html:
print i
html = soup.prettify(utf-8)
with open(output1.html, wb)作为文件:
file.write(html)
因为我用汤。美化,它会生成如下所示的html:
< p>
< strong>
BATAM.TRIBUNNEWS.COM,BINTAN
< / strong>
- Tradisi pedang pora mewarnai serah terima jabatan pejabat di
Polres
< / a>
< a href =http://batam.tribunnews.com/tag/bintan/title =民丹岛>
民丹
< / a>
,Senin(3/10/2016)。
< / p>
我想得到如下结果: print i
:
< p>< strong> BATAM.TRIBUNNEWS.COM,BINTAN< / strong> - Tradisi pedang pora mewarnai serah terima jabatan pejabat di< a href =http://batam.tribunnews.com/tag/polres/title =Polres> Polres< / a> < a href =http://batam.tribunnews.com/tag/bintan/title =Bintan>民丹< / a>,Senin(3/10/2016)。< / p>
< p> Empat perwira baru Senin itu diminta cepat bekerja。 Tumpukan pekerjaan rumah sudah menanti di meja masing masing。< / p>
如何得到与相同的结果print i
(也就是说,标签和它的内容出现在同一行上)?谢谢。
与open(output1.html,w)作为文件:
file.write(str(soup))
I modified an html file by removing some of the tags using beautifulsoup
. Now I want to write the results back in a html file.
My code:
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)
Since I used soup.prettify, it generates html like this:
<p>
<strong>
BATAM.TRIBUNNEWS.COM, BINTAN
</strong>
- Tradisi pedang pora mewarnai serah terima jabatan pejabat di
<a href="http://batam.tribunnews.com/tag/polres/" title="Polres">
Polres
</a>
<a href="http://batam.tribunnews.com/tag/bintan/" title="Bintan">
Bintan
</a>
, Senin (3/10/2016).
</p>
I want to get the result like print i
does:
<p><strong>BATAM.TRIBUNNEWS.COM, BINTAN</strong> - Tradisi pedang pora mewarnai serah terima jabatan pejabat di <a href="http://batam.tribunnews.com/tag/polres/" title="Polres">Polres</a> <a href="http://batam.tribunnews.com/tag/bintan/" title="Bintan">Bintan</a>, Senin (3/10/2016).</p>
<p>Empat perwira baru Senin itu diminta cepat bekerja. Tumpukan pekerjaan rumah sudah menanti di meja masing masing.</p>
How can I get a result the same as print i
(ie. so the tag and its content appear on the same line)? Thanks.
解决方案
Just convert the soup
instance to string and write:
with open("output1.html", "w") as file:
file.write(str(soup))
这篇关于如何使用Python BeautifulSoup将输出写入html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文