Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
XML parsing from URL
#4
(Nov-21-2018, 07:37 AM)stranac Wrote: Your url_to_parse holds the contents of the xml file, and .parse() expects a path or an open file.
You should either pass the response object to .parse() (and not the data read from it), or use .fromstring() instead.

Despite this, I am running into a conundrum:

Quote:Please enter an XML URL to parse: http://py4e-data.dr-chuck.net/comments_42.xml
Traceback (most recent call last):
File "/home/lamidotijjo/Documents/PythonProjects/parsexml.py", line 48, in <module>
tree = ET.parse(url)
File "/usr/lib/python3.6/xml/etree/ElementTree.py", line 1196, in parse
tree.parse(source, parser)
File "/usr/lib/python3.6/xml/etree/ElementTree.py", line 586, in parse
source = open(source, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'http://py4e-data.dr-chuck.net/comments_42.xml'

Here is the modified code:
import urllib.parse, urllib.request, urllib.error
import xml.etree.ElementTree as ET

list_of_ints = []
url = input('Please enter an XML URL to parse: ')
# url_to_open = urllib.request.urlopen(url).read()

tree = ET.parse(url)
root = tree.getroot()
lst = root.findall('comment')
for item in lst:
   print(item.find('count'))
Reply


Messages In This Thread
XML parsing from URL - by mightyn00b - Nov-21-2018, 03:28 AM
RE: XML parsing from URL - by stranac - Nov-21-2018, 07:37 AM
RE: XML parsing from URL - by mightyn00b - Nov-22-2018, 12:45 AM
RE: XML parsing from URL - by snippsat - Nov-21-2018, 04:34 PM
RE: XML parsing from URL - by mightyn00b - Nov-22-2018, 02:22 AM
RE: XML parsing from URL - by Larz60+ - Nov-22-2018, 02:59 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020