我试图解析从url检索到的XML文件有点困难,我的目标是将这个xml文件放到一个结构良好的对象中,以便轻松检索其数据.我当前的代码导致以下错误:
>>> tree = etree.parse(data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "lxml.etree.pyx", line 3299, in lxml.etree.parse (src/lxml/lxml.etree.c:72421)
File "parser.pxi", line 1791, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:105883)
File "parser.pxi", line 1817, in lxml.etree._parseDocumentFromURL (src/lxml/lxml.etree.c:106182)
File "parser.pxi", line 1721, in lxml.etree._parseDocFromFile (src/lxml/lxml.etree.c:105181)
File "parser.pxi", line 1122, in lxml.etree._BaseParser._parseDocFromFile (src/lxml/lxml.etree.c:100131)
File "parser.pxi", line 580, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:94254)
File "parser.pxi", line 690, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:95690)
File "parser.pxi", line 618, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:94722)
OSError: Error reading file '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
码:
(scraper) gmf:scr gmf$python3
Python 3.4.2 (default, Jan 2 2015, 20:14:16)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib.request
>>> from lxml import etree
>>>
>>> opener = urllib.request.build_opener()
>>> f = opener.open('https://nordfront.se/feed')
data = f.read()
f.close()
>>> tree = etree.parse(data)
我非常感谢你的帮助
解决方法:
根据doc字符串(请参阅help(ET.parse)),ET.parse需要第一个参数
成为
>文件名/路径
import lxml.etree as ET
tree = ET.parse(filename)
>一个文件对象
with open('data.xml') as f:
tree = ET.parse(f)
>类似文件的对象
import io
tree = ET.parse(io.BytesIO(data))
>使用HTTP或FTP协议的URL
import urllib.request
opener = urllib.request.build_opener()
tree = ET.parse(opener.open(url))
最后一个选项,它将opener.open(url)直接传递给ET.parse而不是定义data = f.read()可能是你想要使用的选项.
或者,当您已经在字符串中包含XML数据时,可以使用ET.fromstring:
root = ET.fromstring(data)
但请注意,解析返回ElementTree,而fromstring返回Element.