代码之家  ›  专栏  ›  技术社区  ›  Alexandros Marinos

在python中使用urllib2的解码问题

  •  0
  • Alexandros Marinos  · 技术社区  · 14 年前

    我正在尝试使用Python2.7中的urllib2从Web获取一个页面。页面碰巧是用Unicode(UTF-8)编码的,并且有希腊字符。当我试图用下面的代码来获取和打印它时,我得到的是胡言乱语而不是希腊字符。

    import urllib2
    print urllib2.urlopen("http://www.pamestihima.gr").read()
    

    结果在Netbeans 6.9.1和Windows7CLI中都是相同的。

    我做错了什么,但怎么了?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Steve Tjoa    14 年前

        2
  •  3
  •   knitti freethinker    14 年前
    1. 'somestring in utf8'.decode('utf-8') == u'somestring in utf-8' u'' ''

    2. utf-8