Python 入门 错误显示 AttributeError: 'module' object has no attribute 'urlencode'
代码是importurllibimporturllib.requesturl='http://www.baidu.com'user_agent='Mozilla/4.0(...
代码是
import urllib
import urllib.request
url = 'http://www.baidu.com'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {'name' : 'WHY',
'location' : 'SDU',
'language' : 'Python' }
headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values)
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req)
the_page = response.read()
F5後出现如下的报错,请问该怎麼改?
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "C:/Users/Window/Desktop/urllib2_test01.py", line 13, in <module>
data = urllib.urlencode(values)
AttributeError: 'module' object has no attribute 'urlencode'
>>> 展开
import urllib
import urllib.request
url = 'http://www.baidu.com'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {'name' : 'WHY',
'location' : 'SDU',
'language' : 'Python' }
headers = { 'User-Agent' : user_agent }
data = urllib.urlencode(values)
req = urllib.request.Request(url, data, headers)
response = urllib.request.urlopen(req)
the_page = response.read()
F5後出现如下的报错,请问该怎麼改?
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "C:/Users/Window/Desktop/urllib2_test01.py", line 13, in <module>
data = urllib.urlencode(values)
AttributeError: 'module' object has no attribute 'urlencode'
>>> 展开
1个回答
展开全部
是python 3版本吧,要用urllib.parse
import urllib.parse
import urllib.request
url = 'http://www.baidu.com/s'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
values = {'name' : 'WHY',
'location' : 'SDU',
'language' : 'Python',
'ie' : 'utf-8',
'wd' : 'python' }
headers = { 'User-Agent' : user_agent }
data = urllib.parse.urlencode(values)
#data=data.encode(encoding='UTF8')
req = urllib.request.Request(url+'?'+data)
#, data, headers)
response = urllib.request.urlopen(req)
the_page = response.read()()
print(the_page.decode('UTF8'))
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询