简体   繁体   中英

Parse XML in Python with lxml.etree

How can I parse this site ( http://www.tvspielfilm.de/tv-programm/rss/heute2015.xml ) with python to get for example the tv programm for today on SAT at 20:15? I've tried the Python library lxml.etree, but I failed:

#!/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

The method Element.findall uses xpath expression as an argument. 'SAT' finds only direct children that are named SAT of the root node, witch is 'rss' . If you need to find a tag anyway in the document use './/SAT' .

The expression './/items' is what you looking for:

#!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM