urllib2 - 迭代非序列 [英] urllib2 - iteration over non-sequence

查看:76
本文介绍了urllib2 - 迭代非序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让urllib2在运行python的服务器上工作

2.2.1。当我运行以下代码时:

import urllib2

for urllib2.urlopen('''www.google.com''):

打印行

i将始终收到错误:

Traceback(最近一次调用最后一次):

文件"< stdin>" ,第1行,在?

TypeError:迭代非序列

任何人有任何答案吗?

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?

推荐答案

rp*****@gmail.com 写道:

我试图让urllib2在运行python的服务器上工作

2.2.1。当我运行以下代码时:


import urllib2

for urllib2.urlopen('''www.google.com''):

打印行


i将始终收到错误:

Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

TypeError:迭代非序列


任何人有任何答案?
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'')
>>import urllib2
urllib2.urlopen(''www.google.com'')



Traceback(大多数)最近的电话最后一次):

文件"<互动输入>",第1行,< module>

文件" C:\Python25 \ lib \urllib2.py",第121行,在urlopen中

返回_opener.open(url,data)

文件" C:\Python25 \lib \\url lib2.py",第366行,打开

protocol = req.get_type()

文件" C:\Python25 \lib \ urllib2.py" ,第241行,在get_type中

引发ValueError,未知网址类型:%s %self .__ original

ValueError:未知网址类型: 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'')
>>import urllib2
urllib2.urlopen(''http://www.google.com'')



< addinfourl at 27659320,其fp =< socket._fileobject对象位于0x01A51F48>>


Python的交互模式对于追踪这种类型非常有用

的问题。


-Larry

<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


感谢Larry的回复,但我仍然遇到麻烦。如果我正确理解你,你只是建议我在地址前添加一个http://

?但是当我运行时:
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
>>import urllib2
site = urllib2.urlopen(''http://www.google.com'')
for line in site:
print line



我仍​​然收到消息:


TypeError:迭代非序列

文件"< stdin>" ,第1行

TypeError:迭代非序列

I am still getting the message:

TypeError: iteration over non-sequence
File "<stdin>", line 1
TypeError: iteration over non-sequence


rp ***** @ gmail.com 写道:

感谢Larry的回复,但我仍然遇到麻烦。如果我正确理解你,你只是建议我在地址前添加一个http://

?但是当我运行时:

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
>>>import urllib2
site = urllib2.urlopen(''http://www.google.com'')
for line in site:
print line



我仍然收到消息:


TypeError:迭代非序列

文件"< stdin>",第1行
TypeError:迭代非序列


I am still getting the message:

TypeError: iteration over non-sequence
File "<stdin>", line 1
TypeError: iteration over non-sequence



较新版本的Python愿意实现一个it />
*读取*的迭代器一个文件对象的内容,并在循环中逐行向你提供行b / b
。但是,你明确地说过你正在使用的
Python的版本,并且早于发生器/迭代器。


所以...你必须明确阅读内容类似文件的对象

你自己,并循环你自己的行。但是,不要害怕 -

这很容易。 socket._fileobject对象提供了一个方法readlines

,它读取对象的* whole *内容,并返回

行的列表。您可以遍历该行列表。像这样:


import urllib2

url = urllib2.urlopen(''http://www.google.com'')

for url.readlines():

打印行

url.close()

Gary Herron


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



这篇关于urllib2 - 迭代非序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆