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

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

import urllib2
print target.hearders #或使用target.info(),获得charset的内容为:utf-8
utf-8格式解码,否则print时会输出乱码
print content #正确解码,显示正常中文