IT박스

URL에서 이스케이프 된 문자 디코딩

itboxs 2020. 10. 26. 07:49
반응형

URL에서 이스케이프 된 문자 디코딩


이스케이프 문자가있는 URL이 포함 된 목록이 있습니다. 이러한 문자는 urllib2.urlopenhtml 페이지를 복구 할 때 설정되었습니다 .

http://www.sample1webpage.com/index.php?title=%E9%A6%96%E9%A1%B5&action=edit
http://www.sample1webpage.com/index.php?title=%E9%A6%96%E9%A1%B5&action=history
http://www.sample1webpage.com/index.php?title=%E9%A6%96%E9%A1%B5&variant=zh 

파이썬에서 이스케이프되지 않은 형태로 다시 변환하는 방법이 있습니까?

추신 : URL은 utf-8로 인코딩됩니다.


공식 문서.

urllib.unquote()

%xx이스케이프를 해당하는 단일 문자로 바꿉니다 .

예 : unquote('/%7Econnolly/')수율 '/~connolly/'.

그리고 디코딩 만하면됩니다.


업데이트 : Python 3의 경우 다음을 작성합니다.

urllib.parse.unquote(url)

Python 3 문서.


그리고 사용하는 경우 다음을 사용할 Python3수 있습니다.

urllib.parse.unquote(url)

또는 urllib.unquote_plus

>>> import urllib
>>> urllib.unquote('erythrocyte+membrane+protein+1%2C+PfEMP1+%28VAR%29')
'erythrocyte+membrane+protein+1,+PfEMP1+(VAR)'
>>> urllib.unquote_plus('erythrocyte+membrane+protein+1%2C+PfEMP1+%28VAR%29')
'erythrocyte membrane protein 1, PfEMP1 (VAR)'

당신이 사용할 수있는 urllib.unquote


import re

def unquote(url):
  return re.compile('%([0-9a-fA-F]{2})',re.M).sub(lambda m: chr(int(m.group(1),16)), url)

참고 URL : https://stackoverflow.com/questions/8136788/decode-escaped-characters-in-url

반응형