[Tutor] urlopen: where are the results?
Alan Gauld
alan.gauld at freenet.co.uk
Thu Mar 23 17:34:04 CET 2006
> >>> urlopen("http://www.kermitrose.com")
> <addinfourl at 13510096 whose fp = <socket._fileobject object at
> 0x00CDD768>>
> should I have assigned a name to the file by
> website = urlopen(" http://www.kermitrose.com" ) ?
Yes thats the idea. Its not really a file, but its what Python calls a
"file-like object"
That is it behaves like a file in that it has the same metjods and
behaviour.
It may not support all of the file methods but it will do the basics.
> And I had not yet found out about the read function, so even if I had
> understood that urlopen returned a file , I still would not have know
> how to see the contents of that file.
Did you look at the url2lib documentation?
If you read it, it tells you that it returns a "file like object"
Also at the bottom there is a link to a page full of examples. The first one
is:
=========================
11.5.22 Examples
This example gets the python.org main page and displays the first 100 bytes
of it:
>>> import urllib2
>>> f = urllib2.urlopen('http://www.python.org/')
>>> print f.read(100)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?xml-stylesheet href="./css/ht2html
========================
Which shows how to open and read a web page.
And if you went to the index and click 'f' you will find a link
to 'file object' which gives you the list of methods you can call
on file objects. (Admittedly not all of them work on file-like
objects, but most will)
I admit learning how to use the Python document set is not intuitive
but it's worth persevering and the Module Index is invaluable.
Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list