[Tutor] Read same instance twice

Kent Johnson kent37 at tds.net
Mon Oct 27 22:36:49 CET 2008


On Mon, Oct 27, 2008 at 4:03 PM, Øyvind <python at kapitalisten.no> 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.

> How can I easiest pick up more information from the site without opening
> it more than once?

Just remember the data. It's like reading a file, you can't read the
same file twice without re-opening, but you can remember the (string)
data from the file and use it however you want to:
data = side.read()
liste = self.compile_obj.findall(data)
liste2 = self.compile_obj2.findall(data)

Kent


More information about the Tutor mailing list