quote()、unquote()和urlencode()

import urllib.request as ureq
import urllib.parse as uparse

# 对url中的特殊字符进行转换
name = 'joe mama'
number = 6
base = 'http://www.baidu.com'
final = '?name=%s&num=%d' % (name, number)
fi = base + ureq.quote(final)
fi2 = ureq.unquote(fi)
print(fi, fi2, sep='\n')
# 字典键值对
aDict = {
     'name': '值 Garcia', 'hmdir': '~ggarcia'}
# 将键值对转化为url的一部分
res = uparse.urlencode(aDict)
print(res)

结果


http://www.baidu.com%3Fname%3Djoe%20mama%26num%3D6
http://www.baidu.com?name=joe mama&num=6
name=%E5%80%BC+Garcia&hmdir=~ggarcia

Process finished with exit code 0

你可能感兴趣的:(python碎碎,python)