将美化组对象写入Python文件

将美化组对象写入Python文件,python,beautifulsoup,Python,Beautifulsoup,我正试图将一个BeautifulSoup对象写入一个文件,但遇到了一个错误,就像一些字符无法识别一样。这是我的密码: def save_soup(filename, s_obj): s_o = s_obj file = open('filename', 'w', encoding='UTF-8') file.write(s_o.text) file.close() 针对测试文件运行时,出现以下错误: AssertionError: <!DOC[2506 c

我正试图将一个BeautifulSoup对象写入一个文件,但遇到了一个错误,就像一些字符无法识别一样。这是我的密码:

def save_soup(filename, s_obj):
    s_o = s_obj
    file = open('filename', 'w', encoding='UTF-8')
    file.write(s_o.text)
    file.close()
针对测试文件运行时,出现以下错误:

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>
AssertionError:!=

除了??,它们完全一样???部分。我做错了什么。谢谢

你需要做这样的事情..只要把它输入字符串就行了

soup = BeautifulSoup(monterey.response.text, "html.parser")
f=open("res.txt","w")
f.write(str(soup))
f.close()

你的问题不完整。这是你的考试吗?断言来自哪里?如何加载文本?这些部件中的任何一个都可能有错误。您还应该提到您正在使用python 3,因为python意味着python 2。请编写一个产生错误的最小代码脚本。即。