爬虫学习-获取页面使用的字符集

前面做爬虫例子的时候打印有时会出现意外的乱码,后来通过查找发现是解码字符集引起的,所以需要实现知道页面使用的字符集才能更好的解码

#encoding=utf-8
#导入需要的包 字符集需要的包是chardet
import requests
import chardet


if __name__=='__main__':
    url = 'http://www.baidu.com'
    #通过get方式打开页面
    response = requests.get(url)
    #获取页面内容
    html = response.content
    #判断页面使用的字符集
    charset = chardet.detect(html)
    #打印输出
    print(charset)

 

你可能感兴趣的:(python相关,python,爬虫,字符集)