[英]Writing a BeautifulSoup object to file Python
I am trying to write a BeautifulSoup object to a file and am getting an error like some characters are unrecognized. 我正在尝试将BeautifulSoup对象写入文件,并且出现错误,例如某些字符无法识别。 Here is my code:
这是我的代码:
def save_soup(filename, s_obj):
s_o = s_obj
file = open('filename', 'w', encoding='UTF-8')
file.write(s_o.text)
file.close()
While running it against a test file here is the error: 针对测试文件运行它时,出现以下错误:
AssertionError: <!DOC[2506 chars]sult:<input class="txtfld2" id="bin3" on[567 chars]html> != <!DOC[2506 chars]sult:��� <input class="txtfld2" id="bin3" on[567 chars]html>
They are exactly the same except the ??? 除了???以外,它们完全相同。 part.
部分。 What am I doing wrong.
我究竟做错了什么。 Thanks!
谢谢!
You need to do something like this..just typecasting it to string will help! 您需要做这样的事情..只是将其类型转换为字符串会有所帮助!
soup = BeautifulSoup(monterey.response.text, "html.parser")
f=open("res.txt","w")
f.write(str(soup))
f.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.