使用pyquery过程中出现乱码怎么办? - V2EX
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
shanshuise
V2EX    Python

使用pyquery过程中出现乱码怎么办?

  •  
  •   shanshuise 2013-01-29 21:27:30 +08:00 5725 次点击
    这是一个创建于 4711 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码是:
    3.py
    # -*- coding=utf-8 -*-
    from pyquery import PyQuery as pq
    import codecs

    d = pq(url='http://www.baidu.com/')
    sep = d('body').html().decode('gb18030').encode('utf-8')
    file = codecs.open('new.html', 'w', 'utf-8')
    file.write(se)
    file.close()


    python 111.py出错信息如下:
    Traceback (most recent call last):
    File "3.py", line 6, in <module>
    sep = d('body').html().decode('gb18030').encode('utf-8')
    UnicodeEncodeError: 'ascii' codec can't encode characters in position 146-149: ordinal not in range(128)
    第 1 条附言    2013-01-30 00:05:00 +08:00
    把原来的换成'http://www.baidu.com/'换成'http://tieba.baidu.com/f?kw=宋时行'后又乱码了,查看发现百度主页是,而贴吧是gbk,pyquery无法自动解码gbk?
    15 条回复    1970-01-01 08:00:00 +08:00
    wong2
        1
    wong2  
       2013-01-29 21:40:54 +08:00
    d('body').html()得到的已经是unicode了,直接encode('utf-8')就行了
    shanshuise
        2
    shanshuise  
    OP
       2013-01-29 22:19:54 +08:00
    @wong2 去掉.decode('gb18030')后还是报错。。

    Traceback (most recent call last):
    File "3.py", line 7, in <module>
    file.write(sep)
    File "/usr/lib/python2.7/codecs.py", line 691, in write
    return self.writer.write(data)
    File "/usr/lib/python2.7/codecs.py", line 351, in write
    data, cOnsumed= self.encode(object, self.errors)
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 146: ordinal not in range(128)
    swulling
        3
    swulling  
       2013-01-29 22:22:18 +08:00
    @shanshuise 去掉所有的encode decode codecs。。。

    open就好,为啥用codecs.open?
    spark
        4
    spark  
       2013-01-29 22:28:29 +08:00
    加入:
    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')


    如不行:
    input_file = unicode(open(file).read(), 'utf-8')
    source = pq(input_file)
    swulling
        5
    swulling  
       2013-01-29 22:29:51 +08:00   1
    @shanshuise 以下两种方法均可
    因为html()输出是unicode,如果用codecs.open,在写入时会自动转换为utf-8,而如果用open,则需要手动转换为utf-8

    sep = d('body').html()
    file = codecs.open('new.html', 'w', 'utf-8')
    file.write(sep)
    file.close()


    sep = d('body').html()
    file = open('new.html', 'w')
    file.write(sep.encode('utf8'))
    file.close()
    swulling
        6
    swulling  
       2013-01-29 22:31:06 +08:00
    而如果既手动转了utf-8,然后在自动转一次,就会报错。。
    013231
        7
    013231  
       2013-01-29 22:33:11 +08:00
    @shanshuise 於用`codecs.open`打的文件, 你入unicode而非str. 入自用你指定的字符.
    shanshuise
        8
    shanshuise  
    OP
       2013-01-29 22:33:27 +08:00
    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')

    在加入以上语句后就可以正常工作了,本来我以为用了codecs.open就可以不再使用上述的语句,不知道是怎么回事。话说编码还是真复杂,晕了。
    013231
        9
    013231  
       2013-01-29 22:37:35 +08:00
    @shanshuise 如果用`setdefaultencoding`解, `codecs.open`就有意了. 直接`open`吧.
    shanshuise
        10
    shanshuise  
    OP
       2013-01-29 22:38:09 +08:00
    @swulling 刚看到,确实因为转两次的原因才报错了。

    但是我转两次utf8加上下边的语句全搞上去倒成功了。

    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')
    swulling
        11
    swulling  
       2013-01-29 22:38:40 +08:00
    @shanshuise the use of sys.setdefaultencoding() has always been discouraged

    不要用这个,上面我回复的两种方法都很优雅,没必要用这种dirty hack
    shanshuise
        12
    shanshuise  
    OP
       2013-01-29 22:39:09 +08:00
    @013231 现在已经了解了。使用codecs.open,去掉etdefaultencoding,去掉原文中的encode后正常。
    shanshuise
        13
    shanshuise  
    OP
       2013-01-29 22:40:05 +08:00
    @swulling 嗯,学习了。多谢。
    spark
        14
    spark  
       2013-01-30 20:22:28 +08:00
    @swulling 不错,学习了
    54dev
        15
    54dev  
       2013-09-12 17:21:05 +08:00
    @swulling pyjquery所有的输出好像都是unicode?
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1000 人在线   最高记录 6679   nbsp;   Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 18:53 PVG 02:53 LAX 10:53 JFK 13:53
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86