I need to convert a web page to XML (using Python 3.4.3
). If I write the contents of the URL to a file then I can read and parse it perfectly but if I try to read directly from the web page I get the following error in my terminal:
File "./AnimeXML.py", line 22, in xml = ElementTree.parse (xmlData) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/xml/etree/ElementTree.py", line 1187, in parse tree.parse(source, parser) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/xml/etree/ElementTree.py", line 587, in parse source = open(source, "rb") OSError: [Errno 36] File name too long:
My python code:
# AnimeXML.py #! /usr/bin/Python # Import xml parser. import xml.etree.ElementTree as ElementTree # XML to parse. sampleUrl = "http://cdn.animenewsnetwork.com/encyclopedia/api.xml?anime=16989" # Read the xml as a file. content = urlopen (sampleUrl) # XML content is stored here to start working on it. xmlData = content.readall().decode('utf-8') # Close the file. content.close() # Start parsing XML. xml = ElementTree.parse (xmlData) # Get root of the XML file. root = xml.getroot() for info in root.iter("info"): print (info.attrib)
Is there any way I can fix my code so that I can read the web page directly into python without getting this error?