gpt4 book ai didi

python-3.x - Python3 写入文件 beautifulsoup

转载 作者:行者123 更新时间:2023-12-05 08:19:04 27 4
gpt4 key购买 nike

我想用这段代码编写 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'))

关于python-3.x - Python3 写入文件 beautifulsoup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31451791/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com