zoukankan      html  css  js  c++  java
  • 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>












  • 相关阅读:
    javascript typeof 和 instanceof 的区别和联系
    || and && 理解
    jquery选择器总结
    overflow-y:auto 回到顶部
    HTML 获取屏幕,浏览器,页面的高度
    height()、innerHeight()、outerHeight()函数的区别详解
    git入门篇-----本地操作
    sublime快捷键
    Atom 和 VSCode 同一天发布神器:实时编码分享
    编辑器插件和配置备份神器--sync setting
  • 原文地址:https://www.cnblogs.com/xuchunlin/p/7814932.html
Copyright © 2011-2022 走看看