最近学习python3爬虫的过程中,遇到一个问题:
当url中含有中文的时候,就会出现以下错误:
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1250, in _send_request
self.putrequest(method, url, **skips)
File "/usr/lib/python3.6/http/client.py", line 1117, in putrequest
self._output(request.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 10-14: ordinal not in range(128)
于是我将代码修改为:
from urllib.parse import quote
import string
……
url = quote(url, save=string.printable)
response = urllib.request.urlopen(url)
……
就可以正常打开该url网页