url標準中只允許一部分ASCII字符,如英文字母、數字字符、部分符號等。其他字符如-*漢字等,應被編碼爲%+兩位的十六進制表示,任何單字節字符都可被編碼(多字節是逐字節編碼)。保留字符(! $ & ' ( ) * + , ; = : / ? @ # [ ])是否編碼
1.R語言實現
#repeated-已經編碼過的url是否被再次編碼
URLencode(URL,reserved=FALSE,repeated=FALSE)
URLdecode(URL)
#xml2包中url_escape/url_unescape
url_escape(x,reserved="")
reserved ="][!$&'()*+,;=:/?@#"
URL<-"https://www.aqistudy.cn/historydata/monthdata.php?city=北京" %>% xml2::url_escape(reserved ="][!$&'()*+,;=:/?@#")
2.Python
import urllib.request as tem
tem.quote("https://www.aqistudy.cn/historydata/monthdata.php?city=北京",safe='//:?')
#解碼
tem.unquote()