输入一个中文关键字,两种转码方式

5年以前  |  阅读数:453 次  |  编程语言:Python 
from urllib.request import urlopen
from urllib.parse import quote,urlencode#两种用来将中文转出%%%%形式的不同方式
key = input('请输入一个查询关键字')
print(quote(key))#转码方式1:将key转成%%%%%

args = {
    'wd' : key,
    'ie' : 'utf-8'
}  #转码方式2(以字典形式): wd=%%%%&ie=utf-8
print(urlencode(args))

#url = 'http://www.baidu.com/s?wd' + quote(key)#对应方式1
url = 'http://www.baidu.com/s?' + urlencode(args)#对应方法2
response = urlopen(url)#get请求
print(response.read().decode())

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8