Python 爬虫编码格式问题 gb2312转换utf8

遇到的问题是:爬取网页得到的结果如下(部分)  里面的中文出现乱码。

  

<!DOCTYPE html>
<html lang='zh-CN'>
<head>
<meta charset='gb2312'>
<meta content='IE=edge' http-equiv='X-UA-Compatible'>
<title>2017Äê11ÔÂ10ÈÕÃâ·Ñ´úÀíip µÚ1Ò³</title>
<meta name="keywords" content="´úÀíip£¬´úÀíip¼ì²â£¬´úÀíipÑéÖ¤£¬¿ÉÓôúÀíip£¬×îдúÀíip£¬½ñÈÕ¿ÉÓôúÀíip£¬Ãâ·Ñ´úÀíip">
<meta name="description" content="ip181ÊÇÒ»¼ÒרΪ´úÀíipʹÓÃÕß´òÔìµÄ´úÀíip¼ì²âƽ̨£¬ÕâÀï²»½öÌṩרҵµÄ´úÀíipÑéÖ¤·þÎñ£¬»¹ÎªÄúÌṩ×îеÄÃâ·Ñ´
úÀíip£¬ÊµÊ±¸üдúÀíip¡£
"> <link href="/ip181.css" media="all" rel="stylesheet" /> </head> <body> <style> .gx{ font-size:14px;color:#666666;margin-left:10px;font-weight:100;

这是 

print response.encoding   

输出结果是 ISO-8859-1


使用的方法是
print response.text.encode('ISO-8859-1').decode(requests.utils.get_encodings_from_content(response.text)[0])

结果:

<!DOCTYPE html>
<html lang='zh-CN'>
<head>
<meta charset='gb2312'>
<meta content='IE=edge' http-equiv='X-UA-Compatible'>
<title>2017年11月10日免费代理ip 第1页</title>
<meta name="keywords" content="代理ip,代理ip检测,代理ip验证,可用代理ip,最新代理ip,今日可用代理ip,免费代理ip">
<meta name="description" content="ip181是一家专为代理ip使用者打造的代理ip检测平台,这里不仅提供专业的代理ip验证服务,还为您提供最新的免费代理ip,实时更新代理ip。">
<link href="/ip181.css" media="all" rel="stylesheet" />
</head>
<body>
<style>












原文地址:https://www.cnblogs.com/xuchunlin/p/7814932.html