python - How can I distinguish percent encoded URL and NOT percent encoded URL? -
i receive 2 pattern url, percent encoded , not percent encoded. code don't work input_url_b
because encoded. how can resolve or have ideas?
from urllib.parse import urlparse, parse_qs, urlencode if __name__ == '__main__': input_url_a = "http://sample.jp/api?v1=aaa&v2=日本語&v3=ccc" input_url_b = "http://sample.jp/api?v1=aaa&v2=%93%fa%96%7b%8c%eav3=ccc" # '%93%fa%96%7b%8c%ea' = '日本語' # pattern ok. parsed = urlparse(input_url_a) query = parse_qs(parsed.query) fixed_url = parsed._replace(query=urlencode(query, doseq=true)).geturl() print(fixed_url) # 'http://sample.jp/api?v3=ccc&v2=%e6%97%a5%e6%9c%ac%e8%aa%9e&v1=aaa' resp = urllib.request.urlopen(fixed_url) # pattern b ng. parsed = urlparse(input_url_b) query = parse_qs(parsed.query) fixed_url = parsed._replace(query=urlencode(query, doseq=true)).geturl() print(fixed_url) # 'http://sample.jp/api?v2=%ef%bf%bd%ef%bf%bd%ef%bf%bd%7b%ef%bf%bd%ef%bf%bdv3%3dccc&v1=aaa' resp = urllib.request.urlopen(fixed_url)
ref: previous question. how can create percent encoded url complete url?
Comments
Post a Comment