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

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

  •  
  •   88 · 2015-03-17 23:54:52 +08:00 · 4265 次点击
    这是一个创建于 3336 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条回复    2015-04-07 00:22:11 +08:00
    yangqi
        1
    yangqi  
       2015-03-17 23:58:44 +08:00
    国内很多网站都是gbk或者gb2312的编码
    icedx
        2
    icedx  
       2015-03-18 00:04:58 +08:00
    你为什么要print 呢
    lixia625
        3
    lixia625  
       2015-03-18 00:09:04 +08:00
    `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  
       2015-03-18 00:31:28 +08:00
    这样
    #!/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  
       2015-03-18 03:19:02 +08:00 via iPhone
    ericls
        6
    ericls  
       2015-03-18 04:20:49 +08:00
    这样:
    ```
    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  
       2015-03-18 05:08:31 +08:00 via Android
    有些网页是经过gzip压缩的,要考虑解压问题,否则就是乱码
    bbking
        8
    bbking  
       2015-03-20 20:04:32 +08:00
    print乱码和console的编码有关,建议写到txt看看
    x14oL
        9
    x14oL  
       2015-03-22 03:00:41 +08:00
    你是在cmd里面输出的吧?
    如果是,因为win下cmd是cpXXX的字符集,所以会乱码的
    需要decode一下
    raincen
        10
    raincen  
       2015-03-27 15:18:25 +08:00
    控制台下只能打印cp936编码,加上

    import sys
    import io

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

    #...
    ming2281
        11
    ming2281  
       2015-04-07 00:22:11 +08:00
    我一般: print(string.encode("utf-8"))
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2460 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 07:08 · PVG 15:08 · LAX 00:08 · JFK 03:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.