Python 2.7中的urllib.request
我可以在Python 3.1中使用urllib.request模块。但是当我用Python 2.7运行同样的程序时,就会出现一个错误,内容大致是这样的:
AttributeError: 'module' object has no attribute 'request'.
我认为这个错误是因为在Python 2.7的urllib中没有request模块。因为我需要使用tweepy,所以我必须继续使用Python 2.7,因为tweepy不支持Python 3。
那么我该如何在Python 2.7中使用urllib.request模块呢?
3 个回答
4
你可以看看这个链接:http://docs.python.org/library/urllib2.html。
urllib2
模块是之前的一个模块,后来在Python 3.0中被拆分成了urllib.request
和urllib.error
这两个模块。
37
你也可以使用 six
这个模块来写可以在 Python 2 和 Python 3 中都能运行的代码:
from six.moves import urllib
# ...
result = urllib.request.urlopen(url)
30
在Python 2中,使用 urllib2.urlopen
来打开网址。