【问题标题】:Requests module 405 Client Error where as urllib3 works fine请求模块 405 客户端错误,其中 urllib3 工作正常
【发布时间】:2020-07-17 09:10:41
【问题描述】:

无法使用请求模块获取数据。

import requests
resp = requests.get("https://www.embassy-worldwide.com")
resp.status_code

以下代码运行良好。

import urllib3
http = urllib3.PoolManager()
r = http.request('GET', 'https://www.embassy-worldwide.com/')
r.status

在 requests.get 命令中是否需要设置任何参数以使其工作?

【问题讨论】:

    标签: python-3.x python-requests urllib3


    【解决方案1】:

    如果您正在抓取或发送请求,则网站需要知道它来自浏览器,因此添加相关标头。

    In [57]: headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}
    
    In [58]: import requests
        ...: resp = requests.get("https://www.embassy-worldwide.com", headers=headers)
        ...: resp.status_code
    Out[58]: 200
    

    【讨论】:

      猜你喜欢
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多