javascript - python.requests爬出来的内容与浏览器看到的不同
天蓬老师
天蓬老师 2017-04-10 18:01:06
[JavaScript讨论组]

用python.requests爬取http://app1.sfda.gov.cn/datas...中的表格数据,但是python.requests返回的内容跟浏览器中看的不同,下面附上代码:

import requests
def testLoadRequest():
    params1 = {
        'tableId': '27',
        'tableName': 'TABLE27',
        'tableView': '%BD%F8%BF%DA%C6%F7%D0%B5',
        'Id': '24583'
    }
    headers1 = {
        'Content-Type': "text/html;encoding=gbk",
        'X-Requested-With': 'XMLHttpRequest'
    }
    url1 = 'http://app1.sfda.gov.cn/datasearch/face3/content.jsp';
    try:
        r = requests.get(url1,params=params1, headers=headers1)
        print(r.text)
        print(r.cookies)
        print(r.status_code)
        print(r.url)
    except Exception as e:
        print(e)
testLoadRequest()

下面是浏览器看到的内容:

但是用python.requests爬到的html内容如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="Cache-Control" content="no-store, no-cache, must-revalidate, post-check=0, pre-check=0"/>
    <meta http-equiv="Connection" content="Close"/>
    <script type="text/javascript">function stringToHex(str) {
        var val = "";
        for (var i = 0; i < str.length; i++) {
            if (val == "")val = str.charCodeAt(i).toString(16); else val += str.charCodeAt(i).toString(16);
        }
        return val;
    }
    function YunSuoAutoJump() {
        var width = screen.width;
        var height = screen.height;
        var screendate = width + "," + height;
        var curlocation = window.location.href;
        if (-1 == curlocation.indexOf("security_verify_")) {
            document.cookie = "srcurl=" + stringToHex(window.location.href) + ";path=/;";
        }
        self.location = "/datasearch/face3/content.jsp?tableView=½ø¿ÚÆ÷е&Id=24583&tableName=TABLE27&tableId=27&security_verify_data=" + stringToHex(screendate);
    }</script>
    <script>setTimeout("YunSuoAutoJump()", 50);</script>
</head>
</html>

很明显爬出来的内容不是表格里的数据,而且有时还会爬不出来报
('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))
这个错误,有知道原因的人吗??希望能给我点明一下,谢谢了

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(1)
高洛峰

帮测试了,请求源存在问题,url1我更换了链接可以抓取成功。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号