如何在python中解析一个大的XML文件?

时间:2013-12-07 22:37:58

标签: python xml parsing python-2.7

我正在使用python版本2.7。*我需要获取youtube播放列表。我是这样做的:

import urllib
from xml.dom import minidom

playlist_xml = str(urllib.urlopen('https://gdata.youtube.com/feeds/api/playlists/PLKwibIpsTqDyV6NgiJmO-x0yKfqWjApwp?v=2').read())
playlist = minidom.parse(playlist_xml)

问题是我无法解析结果......

Traceback (most recent call last):
  File "/Users/Python/parser.py", line 11, in <module>
    playlist = minidom.parse(playlist_xml)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/minidom.py", line 1914, in parse
    return expatbuilder.parse(file)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/expatbuilder.py", line 922, in parse
    fp = open(file, 'rb')
IOError: [Errno 63] File name too long: 

1 个答案:

答案 0 :(得分:3)

您应该使用parseString minidom方法而不是parse。虽然parseString解析XML并接受字符串,但parse接受filename_or_file,并且未能将XML内容视为文件名。

playlist = minidom.parseString(playlist_xml)