python url安全轉碼

import string
from urllib.request import urlopen
from urllib.parse import quote

# parse 解析  quote 引用
# url 不能寫中文 之所以我們能夠在url中看到中文
# 是因爲瀏覽器出於用戶友好的目的
# 但是在url執行的時候 中文會被轉碼
# 如果不進行轉碼 程序會出錯

url = 'http://api.map.baidu.com/telematics/v3/weather?location=鄭州市&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'
response = urlopen(quote(url,safe = string.printable))
#urlopen 不支持中英文混寫
responseData = response.read()
print(responseData)


# 地址欄不支持使用中文,所以需要進行轉碼
# 轉碼的時候 不但會將中文進行轉碼
# 同事也會將一些特殊符號進行轉碼 比如:: ?
# 如果不想讓這些特殊符號進行轉碼
# 就要使用安全轉碼(只會轉碼中文)
print('沒有使用safe\n{}'.format(quote(url)))

print('使用了safe \n{}'.format(quote(url,safe=string.printable)))

結果顯示:

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