requests爬虫请求报错:UnicodeEncodeError: 'latin-1' codec can't encode character '\u2026' in position 30


在爬虫的时候,犯了一个很低级的错误,是关于requests请求报错,原因是因为浏览器在显示User-Agent属性时,应为属性值过长,所以使用了省略号,导致添加过程中造成了编译错误。有图为证:F12开发者工具,查看访问的请求头

具体报错如下:

  1. Traceback (most recent call last):
  2. File "D:/python_code/xxxx/xxxx.py", line 34, in <module> getCarrier()
  3. File "D:/python_code/xxxx/xxxx.py", line 26, in getCarrier
  4. r = requests.get( url, headers=headers)
  5. <span style="font-size:16px;color:#FF0000;">File "D:\Python\Python36\lib\site-packages\requests\api.py", line 72, in get
  6. return request('get', url, params=params, **kwargs)</span>
  7. File "D:\Python\Python36\lib\site-packages\requests\api.py", line 58, in request
  8. return session.request(method=method, url=url, **kwargs)
  9. File "D:\Python\Python36\lib\site-packages\requests\sessions.py", line 508, in request
  10. resp = self.send(prep, **send_kwargs)
  11. File "D:\Python\Python36\lib\site-packages\requests\sessions.py", line 618, in send
  12. r = adapter.send(request, **kwargs)
  13. File "D:\Python\Python36\lib\site-packages\requests\adapters.py", line 440, in send
  14. timeout=timeout
  15. File "D:\Python\Python36\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
  16. chunked=chunked)
  17. File "D:\Python\Python36\lib\site-packages\urllib3\connectionpool.py", line 357, in _make_request
  18. conn.request(method, url, **httplib_request_kw)
  19. File "D:\Python\Python36\lib\http\client.py", line 1239, in request
  20. self._send_request(method, url, body, headers, encode_chunked)
  21. File "D:\Python\Python36\lib\http\client.py", line 1280, in _send_request
  22. self.putheader(hdr, value)
  23. File "D:\Python\Python36\lib\http\client.py", line 1212, in putheader
  24. values[i] = one_value.encode('latin-1')
  25. <span style="font-size:16px;color:#FF0000;">UnicodeEncodeError: 'latin-1' codec can't encode character '\u2026' in position 30: ordinal not in range(256)
  26. </span>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM