1

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
user3531864
  • 167
  • 3
  • 11
  • 1
    If you use findall('SAT') This parser will search for all tags named SAT. Since there isn't such a tag, there would be no result. I recommend you to loop through all items 'item' tag – Vincent Beltman Nov 26 '14 at 13:00

1 Answers1

2

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
Arpegius
  • 5,817
  • 38
  • 53