Python scrapy response 编码问题

2018-04-24 16:23:04 +08:00
 llllllucas
爬淘宝的 https://s.taobao.com/search?data-key=s&data-value=132&ajax=true&callback=jsonp1124&q=%E9%9D%A2%E8%86%9C&s=44 json 数据
用 scrapy Request 得到的 response,做编码处理的时候报:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 516-519: ordinal not in range(128)

但是用 requests 库,取的 response,可以正常 json,大佬们问题出在哪里呀?

scrapy 里用了 ascii, ingore,但是把中文字符干掉了,不可行。
2900 次点击
所在节点    Python
3 条回复
llllllucas
2018-04-24 20:15:25 +08:00
大神们快出来呀,折磨新人 2 天咯。。。各种谷歌都没找到答案,,
llllllucas
2018-04-24 21:08:17 +08:00
找到答案了,,url 前面加个 u ‘ https://’ 就可以了。。。%>_<%
mimvp
2018-05-14 17:39:48 +08:00
@llllllucas 嗯,unicode 转义的问题

```
def parse(self, response):
mimvp_url = response.url # 爬取时请求的 url
body = response.body # 返回网页内容

print("mimvp_url : " + str(mimvp_url))
print("body : " + str(body))

# unicode_body = response.body_as_unicode() # 返回的 html unicode 编码
# print("unicode_body : " + str(unicode_body))
```

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

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

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

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

© 2021 V2EX