天天看点

【Python】URL编码(urlencode)和URL解码(parse_qs)

提要:编码用 urlencode()函数 解码用 (parse_qs)

图片:

【Python】URL编码(urlencode)和URL解码(parse_qs)

代码:

#encoding: utf-8

from urllib import request
from urllib import parse
keyword = {"wd":"庄宿正"}
encode = parse.urlencode(keyword)

print(encode)


print(parse.parse_qs(encode))

url = "https://www.baidu.com/s" + "?" + encode
res = request.urlopen(url)
#print(res.read())
           

结果:

wd=%E5%BA%84%E5%AE%BF%E6%AD%A3
{'wd': ['庄宿正']}