Python3写入文件beautifulsoup [英] Python3 Write to file beautifulsoup

查看:120
本文介绍了Python3写入文件beautifulsoup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用以下代码编写beautifulsoup表单:

I wish to write the beautifulsoup form with this code:

soup = BeautifulSoup(con.content)
f = open('/*/*/Desktop/littletext.rtf','w')
f.write(str(soup))
f.close()

我收到此错误:

回溯(最近通话最近): 在第10行的文件"///Desktop/test123.py"中 f.写(汤) TypeError:必须为str,而不是BeautifulSoup

Traceback (most recent call last): File "///Desktop/test123.py", line 10, in f.write(soup) TypeError: must be str, not BeautifulSoup

有什么办法解决此问题吗?我试图将'soup'转换为字符串,但是没有用-f.write(str(soup))

Any ideas how to fix this? I tried to convert 'soup' to string but didn't work -- f.write(str(soup))

推荐答案

发现我的问题是我必须在

found out my problem is that I had to use 'wb' in

f = open('/*/*/Desktop/littletext.rtf','wb')

f.write(str(soup))

一定是

f.write(soup.encode('utf-8'))

这篇关于Python3写入文件beautifulsoup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆