爬虫 code为200,但页面错误的问题

先放输出结果

E:\Python38\python.exe E:/PycharmProjects/test.py
http://www.szse.cn/api/disc/announcement/annList?random=0.16208973259833276
<Response [200]>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link href="/maintain/images/favicon.ico" rel="shortcut icon" type="image/x-icon">
    <title>深圳证券交易所</title>
    <title>50x</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    html,
    body {
      width: 100%;
      height: 100%;
      position: relative;
      background: #fff;
    }

    #wrap {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      margin: auto;
    }

    #contImg {
      max-width: 100%;
      max-height: 100%;
    }
  </style>
</head>

<body>
  <div id="wrap">
    <img id="contImg" src="">
  </div>
</body>
<script>
  (function () {
    var img = new Image();
    var src = '/maintain/images/50x_b.png';
    var wrap = document.getElementById('wrap');
    var contImg = document.getElementById('contImg');
    var vWidth = window.innerWidth;
    var vHeight = window.innerHeight;


    img.onload = function () {
      window.cartoonIWidth = img.width;
      window.cartoonIHeight = img.height;

      cartoonHImgOnloaded(vWidth, vHeight, wrap, contImg);
    };
    img.src = src;

    window.cartoonHImgOnloaded = function (vWidth, vHeight, wrap, contImg) {
      var wrAspectRatio = cartoonIWidth / cartoonIHeight;
      var wrWidth = cartoonIWidth;
      var wrHeight = cartoonIHeight;

      if (wrWidth < vWidth && wrHeight < vHeight) {
        wrap.style.width = wrWidth + 'px';
        wrap.style.height = wrHeight + 'px';
        contImg.style.height = cartoonIHeight + 'px';
      }

      if (wrWidth >= vWidth) {
        var h = vWidth * .9 / wrAspectRatio;

        if (h <= vHeight) {
          wrap.style.width = '90%';
          contImg.style.height = h + 'px';
          wrap.style.height = h + 'px';
        }
      }

      if (wrHeight >= vHeight) {
        var h = vHeight * .9;
        var w = h * wrAspectRatio;

        if (w <= vWidth) {
          wrap.style.height = '90%';
          contImg.style.height = h + 'px';
          wrap.style.width = w + 'px';
        }
      }

      contImg.src = src;
    }


  })()
</script>
import json
import time
import datetime
import requests

t = time.time()
random = '0.' + str(t).replace(".", '')
url = "http://www.szse.cn/api/disc/announcement/annList?random=" + random
print(url)
headers = {
    "Host": "www.szse.cn",
    "Referer": "http://www.szse.cn/disclosure/bond/notice/index.html",
    "Origin": "http://www.szse.cn",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36",
}
data = {"seDate": ["", ""],
        "channelCode": ["bondinfoNotice_disc"],
        "smallCategoryId": ["013901"],
        "pageSize": 30,
        "pageNum": 1
        }
response = requests.post(url=url, headers=headers, data=json.dumps(data))
print(response)
print(response.content.decode())

</html>

Process finished with exit code 0

代码

import json
import time
import datetime
import requests

t = time.time()
random = '0.' + str(t).replace(".", '')
url = "http://www.szse.cn/api/disc/announcement/annList?random=" + random
print(url)
headers = {
    "Host": "www.szse.cn",
    "Referer": "http://www.szse.cn/disclosure/bond/notice/index.html",
    "Origin": "http://www.szse.cn",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36",
}
data = {"seDate": ["", ""],
        "channelCode": ["bondinfoNotice_disc"],
        "smallCategoryId": ["013901"],
        "pageSize": 30,
        "pageNum": 1
        }
response = requests.post(url=url, headers=headers, data=json.dumps(data))
print(response)
print(response.content.decode())

在这里我们可以看到requests请求是成功的,code为200,但返回的页面确实错误的。

经过多次尝试在不断添加headers中的参数
在这里插入图片描述
最后在添加"Content-Type": "application/json",之后成功获得了数据。
在这里插入图片描述
我想这也是一种反爬手段,你必须要去请求获得的数据类型,才能获得数据。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以在爬虫代码中加入异常处理机制,在尝试访问页面时使用 try-except 语句。如果访问出现异常,则跳过此页面并继续爬取其他页面。例如: ``` try: #code for visiting the page except: #code for skipping the page ``` 你也可以根据特定异常进行处理,例如: ``` try: #code for visiting the page except ConnectionError: #code for skipping the page ``` 这样可以对访问的页面进行判断,若无法访问则不会继续执行。 ### 回答2: 当爬虫遇到无法使用的页面时,我们可以通过使用异常处理来提前跳过该页面或进行判断,而不去访问该页面。异常处理是一种处理程序运行期间出现错误或异常的一种方式。 在爬虫中,常见的无法使用页面的情况有网络连接问题页面不存在或服务器拒绝访问等。当出现这些情况时,我们可以使用try-except代码块来捕获异常并进行处理。 例如,当使用Python进行网页爬取时,我们可以使用requests库来发送HTTP请求获取网页内容。在发起请求时,如果遇到网络连接问题或服务器拒绝访问,requests库会抛出相应的异常,如requests.exceptions.RequestException。 我们可以在发起请求的代码块中使用try-except结构来捕获这些异常,并在except部分提前跳过或进行判断。具体代码如下所示: ```python import requests url = 'http://example.com' # 待爬取的页面URL try: response = requests.get(url) # 在这里进行页面的处理或数据提取操作 except requests.exceptions.RequestException as e: # 如果捕获到异常,则进行相应的处理,如打印错误信息、跳过该页面或其他操作 print('请求异常:', e) # 跳过或做其他处理 ``` 在上述代码中,我们将请求语句放在try中,如果在请求过程中出现任何异常,将在except部分捕获。在捕获到异常时,可以根据具体情况进行处理,例如打印错误信息并跳过该页面。 通过使用try-except结构,我们可以在爬虫遇到无法使用的页面时,提前跳过或进行判断,以保证程序的稳定性和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值