V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2017-07-19 10:28:11 +08:00 · 1795 次点击
    这是一个创建于 2480 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如果不加 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 条回复    2017-07-19 11:48:32 +08:00
    gulangyu
        1
    gulangyu  
    OP
       2017-07-19 10:40:58 +08:00
    gstqc
        2
    gstqc  
       2017-07-19 10:47:07 +08:00   ❤️ 1
    headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel … Gecko/20100101 Firefox/54.0'}


    这个中文的字符是什么鬼

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

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

    Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:54.0) Gecko/20100101 Firefox/54.0
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   814 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:54 · PVG 03:54 · LAX 12:54 · JFK 15:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.