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
llllllucas
V2EX  ›  Python

Python scrapy response 编码问题

  •  
  •   llllllucas · 2018-04-24 16:23:04 +08:00 · 2877 次点击
    这是一个创建于 2165 天前的主题,其中的信息可能已经有所发展或是发生改变。
    爬淘宝的 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,但是把中文字符干掉了,不可行。
    3 条回复    2018-05-14 17:39:48 +08:00
    llllllucas
        1
    llllllucas  
    OP
       2018-04-24 20:15:25 +08:00
    大神们快出来呀,折磨新人 2 天咯。。。各种谷歌都没找到答案,,
    llllllucas
        2
    llllllucas  
    OP
       2018-04-24 21:08:17 +08:00
    找到答案了,,url 前面加个 u ‘ https://’ 就可以了。。。%>_<%
    mimvp
        3
    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))
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   954 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 21:11 · PVG 05:11 · LAX 14:11 · JFK 17:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.