![](/img/trans.png)
[英]Python lxml.etree - Is it more effective to parse XML from string or directly from link?
[英]Parse XML in Python with lxml.etree
如何使用python解析此站点( http://www.tvspielfilm.de/tv-programm/rss/heute2015.xml ),以获取例如今天20:15上SAT的电视节目? 我尝试了Python库lxml.etree,但失败了:
#!/usr/bin/python
import lxml.etree as ET
import urllib2
response = urllib2.urlopen('http://www.tvspielfilm.de/tv-programm/rss/heute2015.xml')
xml = response.read()
root = ET.fromstring(xml)
for item in root.findall('SAT'):
title = item.find('title').text
print title
方法Element.findall
使用xpath表达式作为参数。 'SAT'
仅查找被称为根节点SAT的直接子代,witch是'rss'
。 如果仍然需要在文档中找到标签,请使用'.//SAT'
。
您要查找表达式'.//items'
:
#!/usr/bin/python
import lxml.etree as ET
import urllib2
response = urllib2.urlopen('some/url/to.xml')
xml = response.read()
root = ET.fromstring(xml)
for item in root.findall('.//item'):
title = item.find('title').text
print title
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.