I am wondering why I'm not getting the right XML format when I'm writing my XML answer into a file.
Basically what I'm doing is sending a post request and receiving XML data back, doing something like this:
post_request = s.post('myurl')
soup2 = BeautifulSoup(post_request.text, features='xml')
print(soup2.encode('utf-8'))
f=open("exit.xml","w")
f.write(str(soup2.encode('utf-8')))
f.close
First part does the work correctly but when I write it in my exit.xml
all the data appears in the wrong format. What I mean by that is that everything is following, there is no indentation. Instead of going to the next line, there is \n
between the data. I'm getting something like that:
<part number="1976">\n<name>Windscreen Wiper</name>\n<description>The Windscreen wiper automatically removes rain from your windscreen, if it should happen to splash there. It has a rubber\n <ref part="1977">blade</ref>\n which can be ordered separately if you need to replace it </description>\n </part>\n
Any ideas on how to fix?
CodePudding user response:
Try to add b
flag when you open your file for writing. Also, remove the str(...)
:
with open('exit.xml', 'wb') as f_out: # <-- add 'b' flag (binary)
f_out.write(soup2.encode('utf-8'))