0

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)

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

7 Comments

I got this error AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getroot'
Yeah, my bad. I'll edit my answer
Yeah, I edited it. It should work now.
How to save children to separate variables, say var_a and var_b, I have run for loop to get them, how do i save them for child in root: print(child.tag, child.attrib)
I am not sure what you are saying but I suppose that you may need a dictionary like this dict = {} and in the next line you can do this for child in root: dict[child.tag] = child.attrib.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.