Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

urllib.request in Python 2.7

Tags:

I can use urllib.request module with Python 3.1. But when I execute the same program using Python 2.7, an error comes along the lines of;

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

I believe this error is because theres no request module in urllib for 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.

So how I can use urllib.request module in Python 2.7?

like image 973
Dananjaya Avatar asked Sep 19 '10 13:09

Dananjaya


People also ask

What is Urllib request in Python?

The urllib. request module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more. See also. The Requests package is recommended for a higher-level HTTP client interface.

Is Urllib built in Python 3?

The urllib module in Python 3 allows you access websites via your program. This opens up as many doors for your programs as the internet opens up for you. urllib in Python 3 is slightly different than urllib2 in Python 2, but they are mostly the same.


2 Answers

It is also possible to use six module to make code for both python2 & python3:

from six.moves import urllib # ... result = urllib.request.urlopen(url) 
like image 147
jellyfish Avatar answered Sep 28 '22 06:09

jellyfish


Use urllib2.urlopen for Python 2.

like image 25
SilentGhost Avatar answered Sep 28 '22 05:09

SilentGhost