2.6中文解码成URL
from urllib import request
import urllib
#对中文解码成URL的形式
wed = {"wd":"北京"}
url = "https://www.baidu.com/s?"
urll="&tn=78040160_5_pg&ch=8"
wdd = urllib.parse.urlencode(wed)
url = url+wdd+urll
#构造处理器对象
req = request.Request(url)
#发送请求,并读取内容
response = request.urlopen(req).read().decode()
print(wdd)
print(response)
结果:
wd=%E5%8C%97%E4%BA%AC
<html>
<head>
<script>
location.replace(location.href.replace("https://","http://"));
</script>
</head>
<body>
<noscript><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></noscript>
</body>
</html>