推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
gulangyu
V2EX  ›  Python

Requests 加入 headers 之后出现 'latin-1' 编码错误

  •  1
     
  •   gulangyu · Jul 19, 2017 · 2390 views
    This topic created in 3307 days ago, the information mentioned may be changed or developed.

    如果不加 requests.get() 中不加 headers = headers,一切运行正常。

    加入 headers = headers 后出现如下编码错误,谷歌好久没找到答案。

    UnicodeEncodeError: 'latin-1' codec can't encode character '\u2026' in position 29: ordinal not in range(256)

    https://gist.github.com/anonymous/5675cf38b29cb522452024ae92c33097

    5 replies    2017-07-19 11:48:32 +08:00
    gulangyu
        1
    gulangyu  
    OP
       Jul 19, 2017
    0ZXYDDu796nVCFxq
        2
    0ZXYDDu796nVCFxq  
       Jul 19, 2017   ❤️ 1
    headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel … Gecko/20100101 Firefox/54.0'}


    这个中文的字符是什么鬼

    文件头部加上
    # -*- coding: utf-8 -*-
    bombless
        3
    bombless  
       Jul 19, 2017
    ……你可以编码
    tbag781623489
        4
    tbag781623489  
       Jul 19, 2017 via iPhone   ❤️ 1
    header 的 dict 中应该有中文,我也忘了是 string 前面加个 u 还是.encoding('utf-8')
    gulangyu
        5
    gulangyu  
    OP
       Jul 19, 2017
    @gstqc 😅 确实是 Intel … 的问题,我一直诧异这三个点是什么意思。

    刚细细看了些原来是省略,把它展开后就可以了 😄

    Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:54.0) Gecko/20100101 Firefox/54.0
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   884 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 22:34 · PVG 06:34 · LAX 15:34 · JFK 18:34
    ♥ Do have faith in what you're doing.