Re: [Tutor] urlopen: where are the results?

2006-03-24 Thread Kermit Rose









From: Alan Gauld
Date: 03/23/06 11:34:33
To: Kermit Rose; Danny Yoo
Cc: tutor@python.org
Subject: Re: [Tutor] urlopen: where are the results?


Did you look at the url2lib documentation?




I thought I had, but I did not see the examples.

I did not know enough to make sense of the documentation.

So I thought that I would just tryrunning the urlopenand see what happened.

*


 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=""mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]  


*


 Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld














___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] urlopen: where are the results?

2006-03-24 Thread Alan Gauld
 And if you went to the index and click 'f' you will find a link
 How do I get to the index??

There should be a link at the top of each document page

HTH,

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] urlopen: where are the results?

2006-03-23 Thread Alan Gauld
 Anybody care to comment on the following?
 
 from urllib2 import *

Its considered bad practice to use 
from foo import *
but at the  prompt its not too bad.

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

Looks good. What did you expect?

Alan G
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] urlopen: where are the results?

2006-03-23 Thread Kermit Rose









From: Danny Yoo
Date: 03/23/06 00:08:25
To: Kermit Rose
Cc: tutor@python.org
Subject: Re: [Tutor] urlopen: where are the results?


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

##
from urllib2 import urlopen
##


Thanks.



  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.



Ah so. 

should Ihave assigned a name to the file by
website = urlopen(" http://www.kermitrose.com" ) ?


Since I did not assign a name, would I be able to use any of the information
given in the value of the function to access the file?

***


(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.)

*
Yes. This does make sense.
*


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*


*
Thanks. Basically, I had no idea how to interpret the value of urlopen that python
returned.

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.

I will search for the read function in the tutorial. What hints can you give me for
finding it quickly in the tutorial?



Kermit  [EMAIL PROTECTED] 

















___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] urlopen: where are the results?

2006-03-23 Thread gv
On 3/23/06, Kermit Rose [EMAIL PROTECTED] wrote:

 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.

 I will search for the read function in the tutorial.  What hints can you give 
 me for
 finding it quickly in the tutorial?

You hardly need to read about the read function.  Just use it:

print urlopen(http://www.kermitrose.com;).read()

or

Webfile = urlopen(http://www.kermitrose.com;).read()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] urlopen: where are the results?

2006-03-23 Thread Alan Gauld
  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


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] urlopen: where are the results?

2006-03-22 Thread Danny Yoo


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*

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor