Python3 写入文件 beautifulsoup

Python3 Write to file beautifulsoup

我想用此代码编写 beautifulsoup 表单:

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

我收到这个错误:

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))

发现我的问题是我必须在

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

f.write(str(soup))

必须

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