'module' has no attribute 'urlencode'

When I try to run a Python wiki example related to URL encoding:

>>> import urllib >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) >>> f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query", params) >>> print f.read() 

The second line gives an error:

 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'urlencode' 

What am I missing?

+87
python urllib
Mar 06 '15 at 20:13
source share
4 answers

urllib was split into Python 3. Now the urllib.urlencode() urllib.parse.urlencode() , and the urllib.urlopen() function is now urllib.request.urlopen() .

+200
Mar 06 '15 at 20:16
source share
 import urllib.parse urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) 
+26
Jan 08 '17 at 12:28
source share

You are using Python 2 documents, but write your program in Python 3.

+3
Mar 06 '15 at 20:16
source share

If urlib.parse.urlencode () does not work for you in Python3, try the following.

 import urllib3 urllib3.request.urlencode() 

urllib3 is a new module in python3 compared to urllib

0
Nov 23 '17 at 6:10
source share



All Articles