tony michael
tony michael

Reputation: 147

How to Parse XML from website using python

I'm trying parse XML from URL, but i got error FileNotFoundError

where I'm doing wrong?

Here's my code:

import xml.etree.ElementTree as ET
import requests
url = "http://cs.stir.ac.uk/~soh/BD2spring2022/assignmentdata.php"
params = {'data':'spurpyr'}
response = requests.get (url, params)
xml_content = response.content

tree = ET.parse(xml_content)
root = tree.getroot()
print(root.tag)

Upvotes: 0

Views: 1455

Answers (1)

Tecnical Compute
Tecnical Compute

Reputation: 317

ET.parse parses from a file, instead of ET.parse try using ET.fromstring and that would probably help. I can't test your specific case since the URL you have written is giving me an error. So, try changing your code to this

import xml.etree.ElementTree as ET
import requests
url = "http://cs.stir.ac.uk/~soh/BD2spring2022/assignmentdata.php"
params = {'data':'spurpyr'}
response = requests.get (url, params)
xml_content = response.content

root = ET.fromstring(xml_content)
print(root.tag)

Upvotes: 1

Related Questions