urllib2 - iteration over non-sequence

Larry Bates larry.bates at websafe.com
Sun Jun 10 00:33:11 CEST 2007


rplobue at gmail.com wrote:
> im trying to get urllib2 to work on my server which runs python
> 2.2.1.  When i run the following code:
> 
> 
> import urllib2
> for line in urllib2.urlopen('www.google.com'):
>       print line
> 
> 
> i will always get the error:
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: iteration over non-sequence
> 
> 
> Anyone have any answers?
> 

I ran your code:

>>> import urllib2
>>> urllib2.urlopen('www.google.com')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python25\lib\urllib2.py", line 121, in urlopen
    return _opener.open(url, data)
  File "C:\Python25\lib\urllib2.py", line 366, in open
    protocol = req.get_type()
  File "C:\Python25\lib\urllib2.py", line 241, in get_type
    raise ValueError, "unknown url type: %s" % self.__original
ValueError: unknown url type: www.google.com

Note the traceback.

you need to call it with type in front of the url:

>>> import urllib2
>>> urllib2.urlopen('http://www.google.com')
<addinfourl at 27659320 whose fp = <socket._fileobject object at 0x01A51F48>>

Python's interactive mode is very useful for tracking down this type
of problem.

-Larry



More information about the Python-list mailing list