'module' has no attribute 'urlencode'
Asked Answered
S

3

139

When I try to follow the Python Wiki's 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()

An error is raised on the second line:

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

What am I missing?

Spectrohelioscope answered 6/3, 2015 at 20:13 Comment(1)
The "2" in your URL.Subsidy
E
338

urllib has been split up in Python 3.

The urllib.urlencode() function is now urllib.parse.urlencode(),

the urllib.urlopen() function is now urllib.request.urlopen().

Ennead answered 6/3, 2015 at 20:16 Comment(0)
R
45
import urllib.parse
urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
Rajiv answered 8/1, 2017 at 12:28 Comment(0)
I
5

You use the Python 2 docs but write your program in Python 3.

Inscription answered 6/3, 2015 at 20:16 Comment(1)
Well it is a tag in your problem and the link points to the Python 2 docs.Sensibility

© 2022 - 2024 — McMap. All rights reserved.