第二章 urllib數據庫挖掘2.6中文解碼成URL

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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章