首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用requests.get() python时获得403个错误

使用requests.get() python时获得403个错误
EN

Stack Overflow用户
提问于 2020-02-06 10:01:07
回答 1查看 1.4K关注 0票数 2

当获得几个响应后请求多个URL时,它开始为其他URL提供403错误。

我尝试使用用户代理和代理仍然存在问题。我还尝试了延迟0.5秒。

正在使用-请求版本= 2.22.0

下面是它看起来的样子

下面是什么(r.status_code,r.headers,r.text):

代码语言:javascript
复制
403 {'Allow': 'GET, POST, HEAD, PUT, PATCH, DELETE, OPTIONS', 'Content-Encoding': 'gzip', 'Content-Type': 'text/html; charset=UTF-8', 'Accept-Ranges': 'bytes, bytes, bytes, bytes', 'Content-Length': '1519', 'Date': 'Thu, 06 Feb 2020 10:34:40 GMT', 'Connection': 'keep-alive', 'set-cookie': 'machine_cookie=9581501972230; expires=Wed, 05 Feb 2025 10:34:40 GMT; path=/;', 'X-Served-By': 'cache-sea4466-SEA, cache-maa18327-MAA', 'X-Cache': 'MISS, MISS', 'X-Cache-Hits': '0, 0', 'X-Timer': 'S1580985280.913451,VS0,VE312', 'Vary': 'User-Agent, Accept-Encoding'} <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Access to this page has been denied.</title>
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300" rel="stylesheet">
  <style>
    html, body {
      margin: 0;
      padding: 0;
      font-family: 'Open Sans', sans-serif;
      color: #000;
    }

    .container {
      align-items: center;
      display: flex;
      flex: 1;
      justify-content: space-between;
      flex-direction: column;
      height: 100%;
    }

    .container > div {
      width: 100%;
      display: flex;
      justify-content: center;
    }

    .container > div > div {
      display: flex;
      width: 80%;
    }

    .customer-logo-wrapper {
      padding-top: 2rem;
      flex-grow: 0;
      background-color: #fff;
    }

    .customer-logo {
      border-bottom: 1px solid #000;
    }

    .customer-logo > img {
      padding-bottom: 1rem;
      max-height: 50px;
      max-width: 100%;
    }

    .page-title-wrapper {
      flex-grow: 0;  /* was 2, but that pushed it too far down the page */
    }

    .page-title {
      flex-direction: column-reverse;
    }

    .content-wrapper {
      flex-grow: 5;
    }

    .content {
      flex-direction: column;
    }

    @media (min-width: 768px) {
      html, body {
        height: 100%;
      }
    }
  </style>
  <script>
    window._pxAppId = 'PXxgCxM9By';
    window._pxJsClientSrc = '/xgCxM9By/init.js';
    window._pxHostUrl = '/xgCxM9By/xhr';

    startTime = Date.now();
    window._pxOnCaptchaSuccess = function(isValid){
      var solutionTime = Math.floor((Date.now() - startTime) / 1000);
      var reload = function(){ top.location.reload(); };
      sendEvent("captcha/solved?px_uuid=" + window._pxUuid + "&time_to_solution=" + solutionTime + '&isValid=' + isValid, reload);
      setTimeout(reload, 700);
    };

    function sendEvent(event, onload){
      var xhr = new XMLHttpRequest();
      xhr.open("GET", "/_sa_track/" + event);
      if (onload) xhr.addEventListener("load", onload);
      xhr.send();
    }
  </script>
<script type="text/javascript">window._pxVid = "";window._pxUuid = "47a70d80-48cc-11ea-860b-c96869955a6b";</script></head>
<body>
<section class="container">
  <div class="page-title-wrapper">
    <div class="page-title">
      <h1>Please click “I am not a robot” to continue</h1>
    </div>
  </div>
  <div class="content-wrapper">
    <div class="content">
      <div id="px-captcha"></div>
      <p></p>
      <p>
        To ensure this doesn’t happen in the future, please enable Javascript and cookies in your browser.<br/>
        Is this happening to you frequently? Please <a href="https://seekingalpha.userecho.com?source=captcha">report it on our feedback forum</a>.
      </p>
      <p>
        If you have an ad-blocker enabled you may be blocked from proceeding. Please disable your ad-blocker and refresh.
      </p>
      <p>Reference ID: <span id="refid"></span></p>
    </div>
  </div>
  <script>
    document.getElementById("refid").innerHTML = window._pxUuid;
    sendEvent("captcha/shown?px_uuid=" + window._pxUuid);
  </script>
</section>

<script src="/xgCxM9By/captcha/PXxgCxM9By/captcha.js?a=c&m=0"></script>

</body>
</html>
EN

回答 1

Stack Overflow用户

发布于 2020-02-06 12:26:29

服务器通过显示403 Forbidden HTTP状态代码和captcha来阻止您获取所需的信息,以确保请求是由人发起的,而不是由Python发起的。远程服务很可能暂时禁止您的会话或IP地址。

有一些解决办法可以避免来自服务器的这种禁止,但是不能保证您可以克服的限制。

所以我只能给你一些建议:

  1. 最好使用会话而不是一次性请求,因为它保留了请求之间的状态。
  2. 像浏览器一样使用用户代理。
  3. 适度增加请求之间的冷却时间。
  4. 代理也可以被远程服务器禁止(通常是基于它的IP),所以有时在循环模式下使用多个代理是个好主意。
  5. 您的主要目标是使您的请求看起来像来自普通浏览器的请求。您可以在developer选项卡中检查从浏览器到远程服务器的请求。尝试复制浏览器的行为。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60092257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档