最近在使用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()

python requests返回中文乱码

 

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

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

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

 

python requests返回中文乱码

python requests返回中文乱码

 

相关文章:

  • 2021-07-20
  • 2022-02-13
  • 2022-12-23
  • 2021-12-17
  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
猜你喜欢
  • 2021-10-04
  • 2022-12-23
  • 2021-12-06
  • 2021-11-06
  • 2021-10-19
  • 2022-12-23
  • 2021-09-22
相关资源
相似解决方案