[Tutor] Read same instance twice
Brian C. Lane
bcl at brianlane.com
Mon Oct 27 22:17:08 CET 2008
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Øyvind wrote:
> Hello.
>
> I am trying to gather some information from a webpage:
>
> side = urlopen("http://www.website.no")
> rawstr = r"""spy.target="_top">(.*?)$"""
> rawstr2 = r"""spy.target2="_top">(.*?)$"""
>
> compile_obj = re.compile(rawstr, re.IGNORECASE| re.MULTILINE| re.VERBOSE
> | re.UNICODE)
> compile_obj2 = re.compile(rawstr2, re.IGNORECASE| re.MULTILINE|
> re.VERBOSE | re.UNICODE)
>
> liste = self.compile_obj.findall(side.read())
>
> liste = self.compile_obj2.findall(side.read())
>
> It works like a dream getting the first info, but the second doesn't work.
> The instance is empty.
>
That's because you read all of it and passed it to the first regex.
Change to:
side = urlopen("http://www.website.no").read()
then:
liste = compile_obj.findall(side)
liste = compile_obj2.findall(side)
That reads the site's contents once, then you can do whatever you want
with it in your program. I'm not sure why you had the self. reference to
compile_obj, so mix to fit your circumstances :)
Brian
- --
- ---[Office 68.6F]--[Outside 54.2F]--[Server 100.6F]--[Coaster 69.6F]---
- ---[ LADY MARY (367013060) @ 47 36.3071 -122 23.1817 ]---
Software, Linux, Microcontrollers http://www.brianlane.com
AIS Parser SDK http://www.aisparser.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)
Comment: Remember Lexington Green!
iD8DBQFJBi/UIftj/pcSws0RAjtiAJ45Sp++yj8jUhir6lwehLqRzBJswwCfREh7
J83jy1sN1xf8Gi+dWZs9GNM=
=8YQT
-----END PGP SIGNATURE-----
More information about the Tutor
mailing list