getting infos from a website

Chris Liechti cliechti at gmx.net
Sat Mar 30 16:37:49 EST 2002


Zutroi Zatatakowski <abou at cam.org> wrote in
news:mailman.1017516408.9897.python-list at python.org: 
> Geoff Gerrietts wrote:
> Ok, I can now get the html source of the page I want. It stores it in
> a file:
> c = open('c:/python/tester', 'w')

you're opening the file for write only. use 'w+' for read-write
access. don't forget to use c.seek() to reposition the file pointer
if you want to read what you've just written.

> f =
> urllib.urlopen("http://www.coremud.org/cgi-bin/mycore.pl?stocks=1&font=
> Arial") 

here you get a file like object from which you can retreive the data.
e.g "print f.read()"

> k =
> urllib.urlretrieve("http://www.coremud.org/cgi-bin/mycore.pl?stocks=1&f
> ont=Arial", 'tester')

this creates a file named "tester" your file opened and bound to c
would be in confilct with that file if it were in the same directory
so you don't need c.

either you use f or k but you don't need both.

> c.close()
> 
> # But then I'm trying to read the file and it never outputs what I
> ask: 

because urlretrieve created its own file in the current directory.

> raw_input('press Return->')
> 
> c = open('c:/python/tester', 'r')
> c.read()
> 
> # readline(), read(), etc. don't seem to work because it doesn't
> output the 'tester' file. In this example I close then reopen the
> file, but even if I do not close it first, it doesn't output. Could it
> have something to do with file permission? (even if I'm on windoze -
> the 'tester' file in an 'archive' file).
> Anyone knows what I am doing wrong?

-- 
Chris <cliechti at gmx.net>





More information about the Python-list mailing list