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

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

  •  
  •   yakczh · 2014-06-19 15:35:30 +08:00 · 5457 次点击
    这是一个创建于 3598 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    2015-08-14 11:55:21 +08:00
    lizheming
        1
    lizheming  
       2014-06-19 15:57:18 +08:00
    测试没有问题...
    yhf
        2
    yhf  
       2014-06-19 16:11:56 +08:00
    加上
    import sys
    default_encoding = 'utf-8'
    if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)
    yakczh
        3
    yakczh  
    OP
       2014-06-19 19:00:55 +08:00
    @yhf line 14, in <module>
    print(table)
    UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 597: illegal multibyte sequence
    yhf
        4
    yhf  
       2014-06-19 19:23:47 +08:00 via iPad
    @yakczh 在我电脑上没问题呀 你用urllib2试试
    cbsw
        5
    cbsw  
       2014-06-19 19:41:18 +08:00
    @lizheming 没有问题 +1, Lz是输出到什么地方的,有可能是终端 encoding 问题
    lizheming
        6
    lizheming  
       2014-06-19 20:12:21 +08:00
    @cbsw 看楼主的错误信息,终端无误了,正好前两天碰上了这个问题。

    楼主的问题在于终端使用了`GBK`编码(在Windows CMD或者Sublime(其实也是调用CMD)下就是这样的),字符串是`utf-8`编码,在从`utf-8`转换到`GBK`显示的过程中,由于`utf-8`的字符集比`GBK`要大,所以存在`GBK`无法编码的情况,所以就会报此错误。解决办法就是把终端的编码方式改为`utf-8`或者GBK大字符集`GBK10830`(不确定后面那个数字打正确了,请自行搜索吧-。-)...
    yakczh
        7
    yakczh  
    OP
       2014-06-19 20:49:50 +08:00
    参考 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  
       2014-06-19 21:08:16 +08:00
    table.text.decode("utf-8")
    yakczh
        9
    yakczh  
    OP
       2014-06-19 21:13:36 +08:00
    @imn1 AttributeError: 'str' object has no attribute 'decode'
    imn1
        10
    imn1  
       2014-06-19 21:18:28 +08:00
    直接 print(table.text) 有错误么?
    yakczh
        11
    yakczh  
    OP
       2014-06-19 21:55:29 +08:00
    set PYTHONIOENCODING=utf-8 加这个管用
    tuplock
        12
    tuplock  
       2015-08-14 11:55:21 +08:00
    @yakczh 环境变量加入 PYTHONIOENCODING = utf-8 正解
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5406 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 05:59 · PVG 13:59 · LAX 22:59 · JFK 01:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.