0

I started learning Python earlier today and as my first project I wanted to make a script that shows me today's weather forecast.

My script:

import urllib2, re

url = urllib2.urlopen('http://www.wetter.com/wetter_aktuell/wettervorhersage/heute     /deutschland/oberhausen/DE0007740.html')
html = url.read()
url.close()

x = re.search("""<dl><dd><strong>(?P<uhrzeit>.*)""", html, re.S)
x = re.search("""<dd><span class="degreespan" style="font-weight:normal;">(?P<temp>.*)""",  html, re.S)
print x.group('uhrzeit'), x.group('temp')

I used this as template. When I run this script I get an Index Error no such groups

kenm
  • 23,127
  • 2
  • 43
  • 62
  • 2
    You should really use an HTML parser for parsing HTML not regex. Please see this post http://stackoverflow.com/a/1732454/763505 . – Hyperboreus Apr 24 '14 at 18:53

1 Answers1

1

You are overwriting x.

Maybe you want:

x = re.search("""<dl><dd><strong>(?P<uhrzeit>.*)""", html, re.S)
y = re.search("""<dd><span class="degreespan" style="font-weight:normal;">(?P<temp>.*)""",  html, re.S)
print x.group('uhrzeit'), y.group('temp')

And I can't belive that the site you linked advocates using regular expressions for extracting information from HTML.

Hyperboreus
  • 31,997
  • 9
  • 47
  • 87