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

做页面转码时出现乱码,如何删掉乱码?

  •  
  •   omg21 · 2016-09-01 19:24:24 +08:00 · 1757 次点击
    这是一个创建于 2795 天前的主题,其中的信息可能已经有所发展或是发生改变。
    做页面转码时出现了这个问题,同网站连续几个页面有的能转码,有的报错不能转。经过细致的检查后发现不能转码的页面有乱码,在页面中显示方框。
    拿下面这个例子来说, 0 位 1 位、 3 位 4 位各是一个汉字,问题出在 2 位上, 2 位报错:
    aa = b'\xb8\xad\xa4h\xd0\xc2'
    bb = aa.decode('gbk')
    print(bb)

    UnicodeDecodeError: 'gbk' codec can't decode byte 0xa4 in position 2: illegal multibyte sequence

    我现在想既然系统能确定哪个位置有问题,那在出现问题时把这个位置的字符删掉不就行了吗?
    不知道这个想法可不可行,不知道怎样把系统找出的这个位置传到变量中,请指教。

    aa = b'\xb8\xad\xa4h\xd0\xc2'
    try:
    bb = aa.decode('gbk')
    except UnicodeDecodeError:
    3 条回复    2016-09-02 12:12:06 +08:00
    lovedebug
        1
    lovedebug  
       2016-09-01 19:25:44 +08:00
    正确方式不是应该按页面指定的编码方式解码么- -
    omg21
        2
    omg21  
    OP
       2016-09-01 19:45:11 +08:00
    @lovedebug 页面上的编码就是 gbk ,但现在是有乱码,一个乱码导致整个页面都没法转换
    DarkFenrir
        3
    DarkFenrir  
       2016-09-02 12:12:06 +08:00
    这样行不行

    aa.decode('gbk', 'ignore')
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3014 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:42 · PVG 21:42 · LAX 06:42 · JFK 09:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.