Python “模块”没有属性“urlencode”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28906859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:52:48  来源:igfitidea点击:

'module' has no attribute 'urlencode'

pythonpython-3.xurllib

提问by Croll

When I try to follow the Python Wiki's examplerelated to URL encoding:

当我尝试遵循与 URL 编码相关的Python Wiki 示例时

>>> 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?

我错过了什么?

采纳答案by Kevin

urllibhas been split up in Python 3.

urllib已被拆分Python 3

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

urllib.urlencode()现在的功能是urllib.parse.urlencode()

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

urllib.urlopen()功能是现在urllib.request.urlopen()

回答by User

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

您使用 Python 2 文档,但使用 Python 3 编写程序。

回答by Vijay P R

import urllib.parse
urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})