【问题标题】:python request get a REST and return 404python请求获取REST并返回404
【发布时间】:2018-01-02 13:12:18
【问题描述】:

我有一个 REST 网址,例如:

url='http://xx.xx.xx.xx/server/rest/line/125'

我可以通过命令curl得到正确的返回json,像这样:

curl -i http://xx.xx.xx.xx/server/rest/line/125

但是当我使用 Python3 请求时,它总是返回 404,Python 代码:

import requests
resp = requests.get(r'http://xx.xx.xx.xx/server/rest/line/125')
print(resp.status_code)

谁能告诉我问题?服务器是否阻塞了请求?

【问题讨论】:

标签: python rest curl python-requests


【解决方案1】:

在以下情况下显示 404:

  1. URL 不正确,响应实际上是准确的。
  2. URL 中的尾随空格
  3. 网站可能不喜欢来自 Python 代码的 HTTP(S) 请求。通过添加“www”更改标题。到您的引荐来源网址。

resp = requests.get(r'http://www.xx.xx.xx.xx/server/rest/line/125')

headers = {
       'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
    }
result = requests.get('https://www.transfermarkt.co.uk', headers=headers)

在您的情况下,请尝试选项 #3。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 2017-11-15
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多