[Tutor] urlopen: where are the results?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Mar 23 06:07:48 CET 2006

On Wed, 22 Mar 2006, Kermit Rose wrote:

> Anybody care to comment on the following?
> >>> from urllib2 import *

Don't do this.  *grin*  Using 'from [modulename] import *' is not so good
in Python because there's no warning if one of the contents in the module
is overriding an existing definition.

We can use 'import urllib2', or if we really want urlopen():

from urllib2 import urlopen

> >>> urlopen("http://www.kermitrose.com")
> <addinfourl at 13510096 whose fp = <socket._fileobject object at
> 0x00CDD768>>

This is fine so far.  What we're getting back is a file-like object.  But
Once we have this, we can use all the file-like methods we know about,
like read(), in order to pull out things from the file object.

(Aside: the reason that urlopen() doesn't give us the file's contents
directly is being there may be other things we may want to do besides
read() the whole thing at once.  Concretely, we might want to
progressively scan through the web site's contents without reading the
whole thing in one shot if we're concerned about memory consumption.  Or
we might just open the file and watch for an error, as a check to see that
the file exists on the web site.)

But is there something else that's bothering you?  Because we can't read
minds very well, you'll have to make up for our deficiency by telling us
what is surprising to you.  *grin*

More information about the Tutor mailing list