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

学 python 遇到个问题

  •  
  •   vimutt · 2015-04-18 02:57:09 +08:00 · 2672 次点击
    这是一个创建于 3295 天前的主题,其中的信息可能已经有所发展或是发生改变。
    初学python,照网上爬虫教程练习,
    网页来源编码如果是gb2312下面的代码就没问题,如果是utf8编码,就出现错误提示:
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
    请问我该如何处理呢
    ==============================
    import HTMLParser
    ....................

    request = urllib2.Request (Url)
    m = urllib2.urlopen(request)
    con = m.read()
    my.feed(con)
    ==============================
    8 条回复    2015-04-18 15:23:48 +08:00
    vy0b0x
        1
    vy0b0x  
       2015-04-18 03:13:40 +08:00   ❤️ 1
    'awefa'.decode('utf8')
    vimutt
        2
    vimutt  
    OP
       2015-04-18 03:22:06 +08:00 via iPad
    @vy0b0x 我试过con.decode('UTF8') 虽然不提示出错信息了 但是htmlparser 却不输出对网页提取信息的内容 但是gb2312的网页却可以正常提取
    linkiosk
        3
    linkiosk  
       2015-04-18 07:25:43 +08:00 via Android   ❤️ 1
    reload(sys)
    设置默认编码
    vimutt
        4
    vimutt  
    OP
       2015-04-18 11:23:08 +08:00
    @linkiosk 这个也试了 设置默认编码为utf8 不提示出错了,但是htmlparser 依然不输出任何结果 但是gb2312的网页却可以
    vimutt
        5
    vimutt  
    OP
       2015-04-18 12:22:16 +08:00
    完整代码如下 :我又用v2ex试了下 虽然都是utf8编码 ,v2ex就可以输出结果,我想爬的这个站就不行 麻烦各位帮忙看看:
    # -*- coding:utf-8 -*-
    import urllib2
    import HTMLParser

    class MyParser(HTMLParser.HTMLParser):

    def __init__(self):
    HTMLParser.HTMLParser.__init__(self)

    def handle_starttag(self, tag, attrs):
    if tag == 'a':
    for name, value in attrs:
    if name == 'href':
    print value


    my = MyParser()
    Url = 'http://blog.sina.com.cn/s/articlelist_1743100694_0_1.html/'
    Url2 = 'https://v2ex.com/'

    print 'Url:',Url
    request = urllib2.Request (Url)
    m = urllib2.urlopen(request)
    con = m.read()
    my.feed(con.decode('utf-8'))
    bugcoder
        6
    bugcoder  
       2015-04-18 14:29:09 +08:00   ❤️ 1
    @vimutt
    测试了一下,要去掉评论
    pattern = r"<!.*>"
    con = re.sub(pattern, '', m.read().decode('utf-8'))
    vy0b0x
        7
    vy0b0x  
       2015-04-18 14:56:45 +08:00   ❤️ 1
    @vimutt v2ex能输出别的不能输出那就不是编码的事 是解析器的事 你换个解析器 有个HTML5的解析器 纯PYTHON实现的 容错是现有最好的 就是比较慢 还有 推荐用BEAUTIFULSOUP来做这事
    vimutt
        8
    vimutt  
    OP
       2015-04-18 15:23:48 +08:00
    @bugcoder
    @vy0b0x
    感谢 我去试试
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3295 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:20 · PVG 20:20 · LAX 05:20 · JFK 08:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.