【问题标题】:Forbidden (403) when requesting a page via python3通过 python3 请求页面时被禁止(403)
【发布时间】:2020-11-21 11:49:26
【问题描述】:

我想通过 python3 请求this

这是代码:

import requests
bizportal_company_url = "https://www.bizportal.co.il/realestates/quote/generalview/373019"
self.page = requests.get(self.bizportal_company_url)

我得到:

<Response [403]>

当我将 verify=False 添加到 get 命令时,我得到:

InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)
<Response [403]>

我该如何解决?当我访问网址时,没有密码或任何东西。

【问题讨论】:

  • 您确定该网站在网络浏览器中打开吗?
  • @Ava 我可以,是的。为什么,你不能?
  • 不,我不能。 :) 至少与代码中的链接无关。
  • 在获取内容时尝试添加适当的 http 标头。
  • 什么意思?

标签: python-3.x web-scraping python-requests google-colaboratory


【解决方案1】:

您的代码中可能缺少一些内容。

试试这个:

import requests

headers = {
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"
}

session = requests.Session()
response = session.get("https://www.bizportal.co.il", headers=headers)

url = "https://www.bizportal.co.il/realestates/quote/generalview/373019"
print(session.get(url, headers=headers).status_code)

这应该打印出来:

200

这基本上意味着请求已经成功。

【讨论】:

  • 这在 google colab 上不起作用,我得到了超时。你知道为什么吗?
猜你喜欢
  • 2022-01-18
  • 1970-01-01
  • 2019-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 2018-06-13
  • 2017-05-01
相关资源
最近更新 更多