Parsing XML from websites and save the code?
<p>I would like to parse the xml code from a website like 
<a href="http://ops.epo.org/3.1/rest-services/published-data/publication/docdb/EP1000000/biblio" rel="nofollow">http://ops.epo.org/3.1/rest-services/published-data/publication/docdb/EP1000000/biblio</a>
and save it in another xml or csv file.</p>

<p>I tried it with this:</p>

<pre><code>import urllib.request 
web_data = urllib.request.urlopen("http://ops.epo.org/3.1/rest-services/published-data/publication/docdb/EP1000000/biblio")
str_data = web_data.read()
try:
   f = open("file.xml", "w")
   f.write(str(str_data))
   print("SUCCESS")
except:
   print("ERROR")
</code></pre>

<p>But in the saved XML data is between every element '\n' and at the beginning ' b' '</p>

<p>How can i save the XML data without all the 'n\' and ' b' '?</p>

<p><code>read()</code> returns data as <code>bytes</code> but you can save data without converting to <code>str()</code>. You have to open file in <code>byte</code> mode - <code>"wb"</code> - and write data.</p>

<pre><code>import urllib.request

web_data = urllib.request.urlopen("http://ops.epo.org/3.1/rest-services/published-data/publication/docdb/EP1000000/biblio")
data = web_data.read()

try:
   f = open("file.xml", "wb")
   f.write(data)
   print("SUCCESS")
except:
   print("ERROR")
</code></pre>

<p>BTW: To convert <code>bytes</code> to <code>string/unicode</code> you have to use ie. <code>decode('utf-8')</code> .
If you use <code>str()</code> then Python uses own method to create string and it adds <code>b"</code> to inform you that you have <code>bytes</code> in your <code>data</code>.</p>

python xml python-3.5