推荐学习书目
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 · Apr 24, 2018 · 3712 views
    This topic created in 2950 days ago, the information mentioned may be changed or developed.
    爬淘宝的 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 replies    2018-05-14 17:39:48 +08:00
    llllllucas
        1
    llllllucas  
    OP
       Apr 24, 2018
    大神们快出来呀,折磨新人 2 天咯。。。各种谷歌都没找到答案,,
    llllllucas
        2
    llllllucas  
    OP
       Apr 24, 2018
    找到答案了,,url 前面加个 u ‘ https://’ 就可以了。。。%>_<%
    mimvp
        3
    mimvp  
       May 14, 2018
    @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))
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   940 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 20:17 · PVG 04:17 · LAX 13:17 · JFK 16:17
    ♥ Do have faith in what you're doing.