I am trying to write a BeautifulSoup object to a file and am getting an error like some characters are unrecognized. 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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.