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

模拟登录这个网站获取的中文信息出现乱码

  •  
  •   xu123456789 · Nov 20, 2015 · 3011 views
    This topic created in 3851 days ago, the information mentioned may be changed or developed.
    该如何解决?
    百度上的方法基本上没效果
    python 2.7
    使用的是 requests
    15 replies    2015-11-23 00:39:10 +08:00
    function007
        1
    function007  
       Nov 20, 2015
    转个码看看
    haozhang
        2
    haozhang  
       Nov 20, 2015
    看一下 http response 的 content-type 首部字段里面的编码是什么。
    haozhang
        3
    haozhang  
       Nov 20, 2015
    然后解析的时候设置相同的编码。
    loudis
        4
    loudis  
       Nov 20, 2015
    gzip?
    ericls
        5
    ericls  
       Nov 20, 2015
    用 cchardet 看看编码
    meloncrashed
        6
    meloncrashed  
       Nov 20, 2015 via iPhone
    用 3.4 可能好些。
    xu123456789
        7
    xu123456789  
    OP
       Nov 21, 2015
    'Content-Type': 'text/html; charset=UTF-8'
    用这个解码 html.content.decode('utf-8')
    报下面错误
    UnicodeEncodeError: 'gbk' codec can't encode character u'\u203a' in position 15440: illegal multibyte sequence
    xu123456789
        8
    xu123456789  
    OP
       Nov 21, 2015
    @haozhang 'Content-Type': 'text/html; charset=UTF-8'
    用这个解码 html.content.decode('utf-8')
    报下面错误
    UnicodeEncodeError: 'gbk' codec can't encode character u'\u203a' in position 15440: illegal multibyte sequence
    xu123456789
        9
    xu123456789  
    OP
       Nov 21, 2015
    @meloncrashed python3 好多东西不支持
    xu123456789
        10
    xu123456789  
    OP
       Nov 21, 2015
    有人能写个模拟登录 V2EX 不出乱码的代码给我吗?
    xu123456789
        11
    xu123456789  
    OP
       Nov 21, 2015
    @ericls 怎么使用
    DEMONHUNTER
        12
    DEMONHUNTER  
       Nov 21, 2015
    # -*- coding:utf8 -*-

    import sys
    import urllib, urllib2


    def login(username, password):

    url = 'http://www.v2ex.com/signin'

    param = {
    'u': username,
    'p': password,
    'once':48203,
    'next':'/'
    }

    data = urllib.urlencode(param)
    headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,zh-TW;q=0.2,ko;q=0.2,ja;q=0.2',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    'Content-Length': len(data),
    'Content-Type': 'application/x-www-form-urlencoded',
    'Host': 'www.v2ex.com',
    'Origin': 'http://www.v2ex.com',
    'Referer': 'http://www.v2ex.com/signin',
    'Upgrade-Insecure-Requests': 1,
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36'
    }

    response = urllib2.urlopen(url, data, timeout=10)
    the_page = response.read()
    print the_page


    if __name__ == '__main__':
    username = 'xu123456789'
    password = '***********'
    login(username, password)
    DEMONHUNTER
        13
    DEMONHUNTER  
       Nov 21, 2015
    妈蛋的缩进都乱了。。 sorry ,讲究着看吧,没几行。
    vitovan
        14
    vitovan  
       Nov 21, 2015
    嘿,看这里: http://jscup.io/cup/b

    silentsolo
        15
    silentsolo  
       Nov 23, 2015 via iPad
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2891 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 12:35 · PVG 20:35 · LAX 05:35 · JFK 08:35
    ♥ Do have faith in what you're doing.