我在处理请求时遇到一个奇怪的问题,我过去多次使用此代码来验证我的标头是否已更改,但是突然我在基本网站上遇到了Unicode错误,不确定我在做什么错。 我知道用户代理不是“正确”的,但仍然可以正常工作
import requests
headers = {'user-agent':'Mozilla/5.0 (X11; Ubuntu; Linu…) Gecko/20100101 Firefox/69.0'}
session = requests.Session()
session.headers.update( headers)
r = session.get('http://httpbin.org/anything')
print(r.text)
错误:
Traceback (most recent call last):
File "/home/timmy/scrapy_tut/fishing/fishing/spiders/insta.py", line 7, in <module>
r = session.get('http://httpbin.org/anything')
File "/home/timmy/.local/lib/python3.6/site-packages/requests/sessions.py", line 546, in get
return self.request('GET', url, **kwargs)
File "/home/timmy/.local/lib/python3.6/site-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/home/timmy/.local/lib/python3.6/site-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/home/timmy/.local/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
timeout=timeout
File "/home/timmy/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 603, in urlopen
chunked=chunked)
File "/home/timmy/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 355, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3.6/http/client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1280, in _send_request
self.putheader(hdr, value)
File "/usr/lib/python3.6/http/client.py", line 1212, in putheader
values[i] = one_value.encode('latin-1')
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2026' in position 30: ordinal not in range(256)