Python爬虫 - 获取网页编码

前言:在我们做爬虫的时候会发现不同的网页会有不同的编码方式,但我们需要根据编码方式来获取数据,所以我们要先获取编码方式,为此我使用requests库解析当前页的编码方式。

requests库是常用的网页解析库,也是我做爬虫时一直使用的库。其中的apparent_encoding方法可以正确获取当前网页的编码方式。

import requests
url = 'https://www.baidu.com/'
html = requests.get(url)
print(html.apparent_encoding)

在这里插入图片描述

posted @ 2020-01-11 20:53  程序猿杂记  阅读(101)  评论(0)    收藏  举报