urllib2 - iteration over non-sequence
Gary Herron
gherron at islandtraining.com
Sat Jun 9 19:31:53 EDT 2007
rplobue at gmail.com wrote:
> Thanks for the reply Larry but I am still having trouble. If i
> understand you correctly, your are just suggesting that i add an http://
> in front of the address? However when i run this:
>
>
>>>> import urllib2
>>>> site = urllib2.urlopen('http://www.google.com')
>>>> for line in site:
>>>> print line
>>>>
>
> I am still getting the message:
>
> TypeError: iteration over non-sequence
> File "<stdin>", line 1
> TypeError: iteration over non-sequence
>
Newer version of Python are willing to implement an iterator that
*reads* the contents of a file object and supplies the lines to you
one-by-one in a loop. However, you explicitly said the version of
Python you are using, and that predates generators/iterators.
So... You must explicitly read the contents of the file-like object
yourself, and loop through the lines you self. However, fear not --
it's easy. The socket._fileobject object provides a method "readlines"
that reads the *entire* contents of the object, and returns a list of
lines. And you can iterate through that list of lines. Like this:
import urllib2
url = urllib2.urlopen('http://www.google.com')
for line in url.readlines():
print line
url.close()
Gary Herron
More information about the Python-list
mailing list