Python3는 urllib.request는 중국과 링크를 읽어

두 가지 방법은, 하나는 중국, 다음 문자열 연결을 분리 처리, 다른 하나는 수정 될 수있는 직접 링크입니다.

첫 번째 방법은, 중국을 분리

# -*- coding:utf-8 -*-

from urllib.parse import quote

url = 'http://www.example.com/api.php?text=中文在这里'

x = '中文在这里'
x = quote(x)
print(x)
y = 'http://www.example.com/api.php?text='
print(y + x)

결과는 다음과 같다 있습니다

%E4%B8%AD%E6%96%87%E5%9C%A8%E8%BF%99%E9%87%8C
http://www.example.com/api.php?text=%E4%B8%AD%E6%96%87%E5%9C%A8%E8%BF%99%E9%87%8C

두 번째 방법은, 직접 링크 처리

# -*- coding:utf-8 -*-

from urllib.parse import quote


url = 'http://www.example.com/api.php?text=中文在这里'

# 不带附加参数
print('\n不带附加参数:\n%s' % quote(url))

# 附带不转换字符参数
print('\n附加不转换字符参数:\n%s' % quote(url, safe='/:?=&'))

결과는 다음과 같다 있습니다

不带附加参数:
http%3A//www.example.com/api.php%3Ftext%3D%E4%B8%AD%E6%96%87%E5%9C%A8%E8%BF%99%E9%87%8C

附加不转换字符参数:
http://www.example.com/api.php?text=%E4%B8%AD%E6%96%87%E5%9C%A8%E8%BF%99%E9%87%8C

다음과 같이 인용 가능한 매개 변수는 다음과 같습니다

quote(string, safe='/', encoding=None, errors=None)

상기 식에서 사용 가능한 매개 변수의 안전한 범위

reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
참고 웹 사이트 : HTTPS : //www.zhihu.com/question/22899135

추천

출처blog.csdn.net/caorya/article/details/80292221