获取网页编码的方式有很多,一般在head标签中都有定义
response.apparent_encoding可以推测编码类型
如果上述方法还是不准确可以使用py的第三方库
安装模块
pip install cchardet
cchardet.datect()可以传递二进制数据和文本类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| def start_claw(self, offset=0): """ 开始爬取 :param self: :param offset: :return: """ headers = self.headers
url = self.url.format(offset=offset)
headers = myUtils.headers_to_dict(headers) r = requests.get(url, headers=headers, verify=False, proxies=self.__get_ip_proxy()) r.raise_for_status() encoding = cchardet.detect(r.content)['encoding'] r.encoding = encoding json_result = r.json()
|