[Tutor] urllib problem

Evert Rol evert.rol at gmail.com
Tue Oct 12 14:54:34 CEST 2010


> I have this program :
> 
> import urllib
> import re
> f = urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=6")
> inhoud = f.read()
> f.close()
> nummer = re.search('[0-9]', inhoud)
> volgende = int(nummer.group())
> teller = 1 
> while teller <= 3 :
>      url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=" + str(volgende)
>      f = urllib.urlopen(url)
>      inhoud = f.read()
>      f.close()
>      nummer = re.search('[0-9]', inhoud)
>      print "nummer is", nummer.group()
>      volgende = int(nummer.group())
>      print volgende
>      teller = teller + 1
> 
> but now the url changes but volgende not.

I think number will change; *unless* you happen to retrieve the same number every time, even when you access a different url.
What is the result when you run this program, ie, the output of your print statements (then, also, print url)?
And, how can url change, but volgende not? Since url depends on volgende.

Btw, it may be better to use parentheses in your regular expression to explicitly group whatever you want to match, though the above will work (since it groups the whole match). But Python has this "Explicit is better than implicit" thing.

Cheers,

  Evert



More information about the Tutor mailing list