码迷,mamicode.com
首页 > 编程语言 > 详细

python requests返回中文乱码

时间:2018-09-20 20:20:41      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:没有   技术分享   bubuko   class   color   get   乱码   res   .com   

  最近在使用python爬取高考分数线时,获得的response里面输出了中文乱码:

from bs4 import BeautifulSoup
import requests

def get_provice_link(url):
    response=requests.get(url)

    print(response.text)
    soup=BeautifulSoup(response.text,lxml)
    print(soup.title)

def main():
    url=http://www.gaokao.com/beijing/fsx/
    get_provice_link(url)

if __name__ == __main__:
    main()

技术分享图片

 

  解决方案是:将response设置编码格式,一般的如果网页中没有标明type格式,一般默认的都是‘ISO-8859-1‘编码,我们只需要把编码格式转为  ‘gb2312‘ 即可

添加一行代码:下面标红的,这样就可以解决。

 response=requests.get(url)
    response.encoding = ‘gb2312‘
    print(response.text)

 

技术分享图片

技术分享图片

 

python requests返回中文乱码

标签:没有   技术分享   bubuko   class   color   get   乱码   res   .com   

原文地址:https://www.cnblogs.com/zll20153246/p/9683002.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!