zoukankan      html  css  js  c++  java
  • python编码问题(2)

    先上代码:

    # -*- coding: utf-8 -*-
    import sys
    import urllib2
    import re
    import chardet
    import sys
    print sys.getdefaultencoding()  
    
    keyword = u'来源:.+[u4e00-u9fa5]+'.encode('CP936')
    
    html = 'http://finance.people.com.cn/money/n/2014/1009/c42877-25798373.html'
    src = urllib2.urlopen(html).read()
    print chardet.detect(keyword)
    print chardet.detect(src)
    match = re.compile(keyword)
    
    list = match.findall( src)
    for line in list:
        print line
    

    在win7上输出是:

    ascii
    {'confidence': 0.73, 'encoding': 'windows-1252'}
    {'confidence': 0.99, 'encoding': 'GB2312'}
    来源:人民网-理财频道 原创稿
    来源:<a href="http://finance.people.com.cn/money/" target="_blank">人民网-理财频道</a></span>    <a href="http://www.people.com.cn/GB/123231/365208/index.html" target="_blank"><img src="/img/2012wbn/images/peopleclienticon.jpg" />手机看新闻
    来源:<a href="http://news.baidu.com/view.html?from=people" target="_blank">百度新闻
    

    windows上的IDLE(python GUI),默认编码为ascii码(第一行);

    CP936 ->  CP1252  , why????

    读取网页的编码貌似取自网页。

    为什么,cp1252的编码能够在gb2312的编码的字符串中找到匹配?

    ============

    解答

    ============

    请保持关注,目前无

  • 相关阅读:
    2018-5-30 总结
    【数据结构系列】线段树(Segment Tree)
    Google Summer of Code 2017 经验谈
    二分查找
    Binary Indexed Tree
    Github-flavored Markdown 导出为 PDF
    Programming Languages
    Select 选择算法
    取样算法
    HTTP Status 500-Servlet.init() for servlet [springmvc] threw exception解决办法
  • 原文地址:https://www.cnblogs.com/Tommy-Yu/p/4150483.html
Copyright © 2011-2022 走看看