推荐学习书目
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
88
V2EX  ›  Python

小白问一个 python 爬虫中文乱码的问题

  •  
  •   88 · Mar 17, 2015 · 5017 views
    This topic created in 4073 days ago, the information mentioned may be changed or developed.

    encoding: utf-8

    import sys
    default_encoding = 'utf-8'
    if sys.getdefaultencoding() != default_encoding:
        reload(sys)
        sys.setdefaultencoding(default_encoding)
    
    import requests
    r = requests.get("http://court.gov.cn/zgcpwsw/bt/xjscjsbtdseszjrmfy/wlmqkqrmfy/ms/201503/t20150313_6914630.htm")
    print r.text.encode('utf-8')
    

    这样输出的中文为乱码,试了网上的各种办法都没用。求问各位大大应该怎么解决。。。

    11 replies    2015-04-07 00:22:11 +08:00
    yangqi
        1
    yangqi  
       Mar 17, 2015
    国内很多网站都是gbk或者gb2312的编码
    icedx
        2
    icedx  
       Mar 18, 2015
    你为什么要print 呢
    lixia625
        3
    lixia625  
       Mar 18, 2015
    `import urllib
    r = urllib.urlopen("http://court.gov.cn/zgcpwsw/bt/xjscjsbtdseszjrmfy/wlmqkqrmfy/ms/201503/t20150313_6914630.htm")
    print r.read().encode('utf-8')
    `
    亲测不乱码
    lerry
        4
    lerry  
       Mar 18, 2015
    这样
    #!/usr/bin/python
    # coding: utf-8

    import requests
    r = requests.get("http://court.gov.cn/zgcpwsw/bt/xjscjsbtdseszjrmfy/wlmqkqrmfy/ms/201503/t20150313_6914630.htm")
    print r.content

    或者

    #!/usr/bin/python
    # coding: utf-8

    import requests
    r = requests.get("http://court.gov.cn/zgcpwsw/bt/xjscjsbtdseszjrmfy/wlmqkqrmfy/ms/201503/t20150313_6914630.htm")
    r.encoding = "utf-8"
    print r.text.encode('utf-8')

    r.content 是网页原始编码,r.text 是decode后的内容,这个网页是utf-8编码,可以直接print

    r.encoding = "utf-8" 手工设置编码,这样text会使用你设置的编码decode

    另外,楼主开头几行改变默认编码是不建议的写法
    Sylv
        5
    Sylv  
       Mar 18, 2015 via iPhone
    ericls
        6
    ericls  
       Mar 18, 2015
    这样:
    ```
    import requests
    r = requests.get("http://court.gov.cn/zgcpwsw/bt/xjscjsbtdseszjrmfy/wlmqkqrmfy/ms/201503/t20150313_6914630.htm")
    r.encoding = 'utf-8'
    print r.text
    ```
    em70
        7
    em70  
       Mar 18, 2015 via Android
    有些网页是经过gzip压缩的,要考虑解压问题,否则就是乱码
    bbking
        8
    bbking  
       Mar 20, 2015
    print乱码和console的编码有关,建议写到txt看看
    x14oL
        9
    x14oL  
       Mar 22, 2015
    你是在cmd里面输出的吧?
    如果是,因为win下cmd是cpXXX的字符集,所以会乱码的
    需要decode一下
    raincen
        10
    raincen  
       Mar 27, 2015
    控制台下只能打印cp936编码,加上

    import sys
    import io

    sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')

    #...
    ming2281
        11
    ming2281  
       Apr 7, 2015
    我一般: print(string.encode("utf-8"))
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1002 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 22:17 · PVG 06:17 · LAX 15:17 · JFK 18:17
    ♥ Do have faith in what you're doing.