美文网首页
python爬虫html中文乱码解决方案

python爬虫html中文乱码解决方案

作者: 485b1aca799e | 来源:发表于2017-06-18 11:09 被阅读0次
  • 解决方案,对获取的page html代码先进行encode("GBK"),然后再使用decode("utf-8")进行解码
  • 例子
#使用post方法来提交用户名和密码登陆 
import urllib2
import urllib

url = 'http://www.dataguru.cn/member.php?mod=logging&action=login'
user_agent = 'Mozilla/5.0'  
values = {'username' : 'python爬虫',  'password' : '123456789' }  #设置账号和密码
data = urllib.urlencode(values)    #将账号和密码编码成url形式
request = urllib2.Request(url,data)  
request.add_header("user-agent","Mozilla/5.0")
response = urllib2.urlopen(request)  
page = response.read().decode("GBK").encode("utf-8")
print(page)


相关文章

网友评论

      本文标题:python爬虫html中文乱码解决方案

      本文链接:https://www.haomeiwen.com/subject/jzeiqxtx.html