你有一个原子饲料在那里,所以我会用feedparser
来处理:
import feedparser
result = feedparser.parse('https://gdata.youtube.com/feeds/api/videos/Ej4_G-E1cAM/comments')
for entry in result.entries:
print entry.author
此打印:
FreebieFM
micromicros
FreebieFM
Sarah Grimstone
FreebieFM
# etc.
Feedparser是一个外部库,但很容易安装。如果您只需要使用标准库,则可以使用ElementTree
API,但要解析Atom提要,您需要将HTML实体包含在解析器中,并且必须处理名称空间(而不是ElementTree
的优点):
from urllib2 import urlopen
from xml.etree import ElementTree
response = urlopen('https://gdata.youtube.com/feeds/api/videos/Ej4_G-E1cAM/comments')
tree = ElementTree.parse(response)
nsmap = {'a': 'http://www.w3.org/2005/Atom'}
for author in tree.findall('.//a:author/a:name', namespaces=nsmap):
print author.text
的nsmap
字典让ElementTree
的a:
前缀转化为正确的命名空间的元素。
感谢编辑后cfreak,我是一个新的! – Freebie 2013-04-04 16:15:10
没问题。欢迎来到Stackoverflow。你可以做的另一件事是添加你迄今为止尝试过的代码。为了让它看起来像我让你的XML只是突出显示它,并点击小按钮(或者把它放在它周围)。 – Cfreak 2013-04-04 16:16:43
你可以在Python中使用一个名为beautifulsoup的工具。这将满足您所有的HTML解析需求。我不会推荐除python以外的任何东西:) – karthikr 2013-04-04 16:16:56