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()