使用urllib2解析html内容,并正常显示中文的方法 分类: python Module 2013-10-31 17:30 294人阅读 评论(0) 收藏

  1. 得到目标html的编码格式
  2. 按照此编码格式去解析html内容,就能够正常解析

import urllib2
target = urllib2.urlopen(" http://www.amazon.cn")
print target.hearders #或使用target.info(),得到charset的内容为:utf-8
content=target.read().decode("utf-8") #此处对内容 进行 utf-8格式解码,不然print时会输出乱码
print content #正确解码,显示正常中文
相关文章
相关标签/搜索