代码出错了,但是不知道错在了那里

response = urllib.request.urlopen(request)这一行代码出错了,咋整啊?

import urllib.request
import urllib.parse
base_url='http://www.baidu.com/s?'
data={
'wb':'周杰伦',
'sex':'男'
}
new_name=urllib.parse.urlencode(data)#使周杰伦的汉字变为unicode编码
print(new_name)#打印新的Unicode码
url = base_url + new_name
print(url)

请求对象的定制

headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
}
request = urllib.request.Request(url = url,headers = headers)
response = urllib.request.urlopen(request)
content=response.read().decode('utf-8')
print(content)

"D:\WMX づJY づ\python work\venv\Scripts\python.exe" "D:/WMX づJY づ/Python Work/file/get请求的urllib.encode的方法.py"
wb=%E5%91%A8%E6%9D%B0%E4%BC%A6&sex=%E7%94%B7
http://www.baidu.com/s?wb=%E5%91%A8%E6%9D%B0%E4%BC%A6&sex=%E7%94%B7
Traceback (most recent call last):
File "D:\WMX づJY づ\Python Work\file\get请求的urllib.encode的方法.py", line 18, in
response = urllib.request.urlopen(request)
File "D:\Python(2022)\lib\urllib\request.py", line 216, in urlopen
return opener.open(url, data, timeout)
File "D:\Python(2022)\lib\urllib\request.py", line 519, in open
response = self._open(req, data)
File "D:\Python(2022)\lib\urllib\request.py", line 536, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "D:\Python(2022)\lib\urllib\request.py", line 496, in _call_chain
result = func(*args)
File "D:\Python(2022)\lib\urllib\request.py", line 1377, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "D:\Python(2022)\lib\urllib\request.py", line 1348, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File "D:\Python(2022)\lib\http\client.py", line 1282, in request
self._send_request(method, url, body, headers, encode_chunked)
File "D:\Python(2022)\lib\http\client.py", line 1293, in _send_request
self.putrequest(method, url, **skips)
File "D:\Python(2022)\lib\http\client.py", line 1131, in putrequest
self._output(self._encode_request(request))
File "D:\Python(2022)\lib\http\client.py", line 1211, in _encode_request
return request.encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\uff1f' in position 6: ordinal not in range(128)

img

文件另存为utf-8格式

我用你提供的代码,运行了一下,好像是正常运行了。我的代码文件是UTF-8编码的。

img

url = parse.quote(line, safe=string.printable)
    # aviod SSL verification
    context = ssl._create_unverified_context()
    # request to open url
    # 'safe' refers to characters that can be ignored
    response = request.urlopen(url, context=context)

因为你得代码url中有无法用ASCII码表示的中文字符,因此需要对url重新编码。python3 中的urllib.parse函数可以解析url,这里可以用来重构url,具体采用其中的 quote 函数。