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

2017-07-19 10:28:11 +08:00
 gulangyu

如果不加 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

1799 次点击
所在节点    Python
5 条回复
gulangyu
2017-07-19 10:40:58 +08:00
0ZXYDDu796nVCFxq
2017-07-19 10:47:07 +08:00
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel … Gecko/20100101 Firefox/54.0'}


这个中文的字符是什么鬼

文件头部加上
# -*- coding: utf-8 -*-
bombless
2017-07-19 10:53:14 +08:00
……你可以编码
tbag781623489
2017-07-19 11:11:48 +08:00
header 的 dict 中应该有中文,我也忘了是 string 前面加个 u 还是.encoding('utf-8')
gulangyu
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

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/376342

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX