python3 下面用 BeautifulSoup 解析 utf8 的 html 报错 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
yakczh

python3 下面用 BeautifulSoup 解析 utf8 的 html 报错

  •  
  •   yakczh Jun 19, 2014 6062 views
    This topic created in 4332 days ago, the information mentioned may be changed or developed.
    import urllib.request as request
    from bs4 import BeautifulSoup

    url='https://github.com/joyent/node/wiki/Projects,-Applications,-and-Companies-Using-Node'
    #url='http://www.cityhouse.cn/default/leaserank.html'
    soup=BeautifulSoup(request.urlopen(url),from_encoding='utf-8')

    tables=soup.find_all('table')
    for table in tables:
    print(table)

    换url='http://www.cityhouse.cn/default/leaserank.html'就正常
    这两个页面都是utf-8编码
    12 replies    2015-08-14 11:55:21 +08:00
    lizheming
        1
    lizheming  
       Jun 19, 2014
    测试没有问题...
    yhf
        2
    yhf  
       Jun 19, 2014
    加上
    import sys
    default_encoding = 'utf-8'
    if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)
    yakczh
        3
    yakczh  
    OP
       Jun 19, 2014
    @yhf line 14, in <module>
    print(table)
    UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 597: illegal multibyte sequence
    yhf
        4
    yhf  
       Jun 19, 2014 via iPad
    @yakczh 在我电脑上没问题呀 你用urllib2试试
    cbsw
        5
    cbsw  
       Jun 19, 2014
    @lizheming 没有问题 +1, Lz是输出到什么地方的,有可能是终端 encoding 问题
    lizheming
        6
    lizheming  
       Jun 19, 2014
    @cbsw 看楼主的错误信息,终端无误了,正好前两天碰上了这个问题。

    楼主的问题在于终端使用了`GBK`编码(在Windows CMD或者Sublime(其实也是调用CMD)下就是这样的),字符串是`utf-8`编码,在从`utf-8`转换到`GBK`显示的过程中,由于`utf-8`的字符集比`GBK`要大,所以存在`GBK`无法编码的情况,所以就会报此错误。解决办法就是把终端的编码方式改为`utf-8`或者GBK大字符集GBK10830`(不确定后面那个数字打正确了,请自行搜索吧-。-)...
    yakczh
        7
    yakczh  
    OP
       Jun 19, 2014
    参考 http://www.newsmth.net/bbsanc.php?path=%2Fgroups%2Fcomp.faq%2FPython%2Ftext%2FPython3000%2FM.1236069588.l0 上面的写法

    for table in tables:
    print(type(table.text))
    sys.stdout.write(table.text.encode("utf-8"))

    输出<class 'str'> sys.stdout.write(table.text.encode("utf-8"))
    TypeError: must be str, not bytes

    table.text输出是str 又提示TypeError: must be str, not bytes
    好奇怪
    imn1
        8
    imn1  
       Jun 19, 2014
    table.text.decode("utf-8")
    yakczh
        9
    yakczh  
    OP
       Jun 19, 2014
    @imn1 AttributeError: 'str' object has no attribute 'decode'
    imn1
        10
    imn1  
       Jun 19, 2014
    直接 print(table.text) 有错误么?
    yakczh
        11
    yakczh  
    OP
       Jun 19, 2014
    set PYTHOnIOENCODING=utf-8 加这个管用
    tuplock
        12
    tuplock  
       Aug 14, 2015
    @yakczh 环境变量加入 PYTHOnIOENCODING= utf-8 正解
    About     Help     Advertise     Blog     API     FAQ     Solana     3672 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 42ms UTC 10:35 PVG 18:35 LAX 03:35 JFK 06:35
    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