Nov-22-2018, 12:45 AM
(Nov-21-2018, 07:37 AM)stranac Wrote: Yoururl_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'))