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

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

  •  
  •   xu123456789 · 2015-11-20 21:44:20 +08:00 · 2278 次点击
    这是一个创建于 3078 天前的主题,其中的信息可能已经有所发展或是发生改变。
    该如何解决?
    百度上的方法基本上没效果
    python 2.7
    使用的是 requests
    15 条回复    2015-11-23 00:39:10 +08:00
    function007
        1
    function007  
       2015-11-20 21:47:37 +08:00
    转个码看看
    haozhang
        2
    haozhang  
       2015-11-20 21:57:29 +08:00
    看一下 http response 的 content-type 首部字段里面的编码是什么。
    haozhang
        3
    haozhang  
       2015-11-20 21:57:58 +08:00
    然后解析的时候设置相同的编码。
    loudis
        4
    loudis  
       2015-11-20 22:30:35 +08:00
    gzip?
    ericls
        5
    ericls  
       2015-11-20 22:51:10 +08:00
    用 cchardet 看看编码
    meloncrashed
        6
    meloncrashed  
       2015-11-20 23:05:14 +08:00 via iPhone
    用 3.4 可能好些。
    xu123456789
        7
    xu123456789  
    OP
       2015-11-21 09:37:07 +08:00
    '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
       2015-11-21 09:37:35 +08:00
    @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
       2015-11-21 09:37:56 +08:00
    @meloncrashed python3 好多东西不支持
    xu123456789
        10
    xu123456789  
    OP
       2015-11-21 09:38:46 +08:00
    有人能写个模拟登录 V2EX 不出乱码的代码给我吗?
    xu123456789
        11
    xu123456789  
    OP
       2015-11-21 09:40:36 +08:00
    @ericls 怎么使用
    DEMONHUNTER
        12
    DEMONHUNTER  
       2015-11-21 11:14:32 +08:00
    # -*- 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  
       2015-11-21 11:16:54 +08:00
    妈蛋的缩进都乱了。。 sorry ,讲究着看吧,没几行。
    vitovan
        14
    vitovan  
       2015-11-21 12:09:13 +08:00
    嘿,看这里: http://jscup.io/cup/b

    silentsolo
        15
    silentsolo  
       2015-11-23 00:39:10 +08:00 via iPad
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5353 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 03:43 · PVG 11:43 · LAX 20:43 · JFK 23:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.