简体   繁体   English

Python 2.7中的urllib.request

[英]urllib.request in Python 2.7

I can use urllib.request module with Python 3.1. 我可以在Python 3.1中使用urllib.request模块。 But when I execute the same program using Python 2.7, an error comes along the lines of; 但是当我使用Python 2.7执行相同的程序时,出现了一个错误:

AttributeError: 'module' object has no attribute 'request'.

I believe this error is because theres no request module in urllib for Python 2.7. 我相信这个错误是因为在urllib中没有Python 2.7的请求模块。 Because I need to use tweepy i'll have to stick with Python 2.7 since tweepy does not support Python 3. 因为我需要使用tweepy,所以我必须坚持使用Python 2.7,因为tweepy不支持Python 3。

So how I can use urllib.request module in Python 2.7? 那么我如何在Python 2.7中使用urllib.request模块呢?

It is also possible to use six module to make code for both python2 & python3: 也可以使用six模块为python2和python3制作代码:

from six.moves import urllib
# ...
result = urllib.request.urlopen(url)

Take a look at http://docs.python.org/library/urllib2.html . 看看http://docs.python.org/library/urllib2.html

The urllib2 module is the predecessor to urllib.request / urllib.error (it has been split into those modules in Python 3.0). urllib2模块是urllib.request / urllib.error的前身(它已被拆分为Python 3.0中的那些模块)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM