# -*- coding:UTF-8 -*-
#Author Chen Da
import urllib.request
import urllib.parse
url = 'http://www.baidu.com/s?'
wd = {'wd':'传智播客'}
#Python3的urlencode需要从parse中调用,可以看到urlencode()接受的是一个字典
print(urllib.parse.urlencode(wd))
#通过urllib.request.unquote()方法,把URL编码字符串,转换回原先字符串
print(urllib.request.unquote('wd=%E4%BC%A0%E6%99%BA%E6%92%AD%E5%AE%A2'))
#做一个百度接口
url = 'http://www.baidu.com/s'
keyword = input('请输入需要查询的字符串: ')
wd = {'wd':keyword}
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0'
}
wd = urllib.parse.urlencode(wd).encode('UTF8')
full_url = url + '?' + wd
request = urllib.request.Request(full_url,headers= headers)
response = urllib.request.urlopen(request)
print(response.read())