首页 > 解决方案 > 使用python中的请求循环分页REST API

问题描述

我是一个非常缺乏 REST API、请求和 python 经验的用户,所以请多多包涵。我已经设法通过 python 使用请求向这个用 Postman 开发的 REST API 抛出一个 GET 请求。我得到的结果很好,但仅限于第一页(只有 50 个项目)。我知道为了获得更多结果,我需要做的就是在 URL 中添加 '?page=2' 等,但不知何故,到目前为止我无法管理循环。在我看来,这个 API 没有“nextUrl”或页面总数功能,所以我有点迷茫。这是我到目前为止所拥有的:

from getpass import getpass
import requests


usr = input('Inform user: ')
pwd = getpass('Inform password : ')


url = 'https://api.inventsys.com.br/v4/login'
payload = "{\n  \"username\": \"" + usr + "\",\n  \"password\": \"" + pwd + "\"\n}"
headers = {
      'Account': 'my-user',
      'Content-Type': 'application/json',

    }
r = requests.request('POST', url, headers = headers, data = payload, allow_redirects=False)

mytoken = r.json()['token']

projectid = input('Inform project ID (10762): ')

url = 'https://api.inventsys.com.br/v4/projects/'+projectid+'/items'
payload = {}
headers = {

      'Account': 'my-user',
      'Token': mytoken,

    }

ativos = requests.request('GET', url, headers = headers, data = payload, allow_redirects=False)


print(ativos.json()['result'])


print(ativos.text)
print(ativos.headers)

它提供了以下结果(我已经缩短了结果,因此它只显示了 50 条记录中的一条):

Inform project ID (10762): 10762
ok
{
    "items": [
        {
            "id": 300457,
            "item_id_parent": null,
            "reference": "",
            "subreference1": "CAMS\/2",
            "subreference2": "CAMS\/2",
            "reference_alpha": null,
            "reference_numeric": null,
            "oid": "CAMS\/2",
            "code": null,
            "code_custom": null,
            "name": "284",
            "image": "https:\/\/static.inventsys.com.br\/278\/thumb\/f-3298939-200x200c.jpg",
            "situations": [],
            "project_id": 10762,
            "project": {
                "id": 10762,
                "name": "Fauna EGR",
                "color": null
            },
            "category_id": 20685,
            "category": {
                "id": 20685,
                "name": "EGR FAUNA - Armadilhas"
            },
            "area_id": null,
            "area": null,
            "location": {
                "lat": -30.137237548828,
                "lng": -50.90788269043,
                "address": {
                    "region": "RS",
                    "city": "Viamão",
                    "district": null,
                    "zipcode": null,
                    "street": "Rodovia Tapir Rocha",
                    "street_number": null,
                    "desc": null,
                    "full": "Rodovia Tapir Rocha Viamão \/ BR"
                }
            },
            "event_last": null,
            "description": null,
            "search_terms": "CAMS\/2 284 Fauna EGR EGR FAUNA - Armadilhas Rodovia Tapir Rocha Viamão \/ BR",
            "info": [
                {
                    "id": 42725,
                    "name": "Observacoes",
                    "type": "longtext",
                    "value": "Ver observações no odk",
                    "fvalue": "Ver observações no odk",
                    "description": null,
                    "ikey": null,
                    "group": "Fauna EGR",
                    "preload": false,
                    "filling": false,
                    "primary": false,
                    "created": "2019-10-01T17:13:04+00:00"
                },
                {
                    "id": 44542,
                    "name": "Data de instalacao",
                    "type": "date",
                    "value": "2019-10-01",
                    "fvalue": "01\/10\/2019",
                    "description": null,
                    "ikey": null,
                    "group": "Fauna EGR",
                    "preload": false,
                    "filling": false,
                    "primary": false,
                    "created": "2019-10-01T17:13:04+00:00"
                },
                {
                    "id": 44543,
                    "name": "IDcartao",
                    "type": "text",
                    "value": "Ste 04",
                    "fvalue": "Ste 04",
                    "description": null,
                    "ikey": null,
                    "group": "Fauna EGR",
                    "preload": false,
                    "filling": false,
                    "primary": true,
                    "created": "2019-10-01T17:13:04+00:00"
                },
                {
                    "id": 44544,
                    "name": "IDcamera",
                    "type": "text",
                    "value": "1",
                    "fvalue": "1",
                    "description": null,
                    "ikey": null,
                    "group": "Fauna EGR",
                    "preload": false,
                    "filling": false,
                    "primary": false,
                    "created": "2019-10-01T17:13:04+00:00"
                },
                {
                    "id": 44545,
                    "name": "IDdobueiro",
                    "type": "text",
                    "value": "284",
                    "fvalue": "284",
                    "description": null,
                    "ikey": null,
                    "group": "Fauna EGR",
                    "preload": false,
                    "filling": false,
                    "primary": false,
                    "created": "2019-10-01T17:13:04+00:00"
                },
                {
                    "id": 44546,
                    "name": "Estrada",
                    "type": "longtext",
                    "value": "Ers-040",
                    "fvalue": "Ers-040",
                    "description": null,
                    "ikey": null,
                    "group": "Fauna EGR",
                    "preload": false,
                    "filling": false,
                    "primary": false,
                    "created": "2019-10-01T17:13:04+00:00"
                },
                {
                    "id": 44547,
                    "name": "Foto Armadilha",
                    "type": "photo",
                    "value": "3298938",
                    "fvalue": "3298938",
                    "description": null,
                    "ikey": null,
                    "group": "Fauna EGR",
                    "preload": false,
                    "filling": false,
                    "primary": false,
                    "created": "2019-10-01T17:13:04+00:00"
                },
                {
                    "id": 44548,
                    "name": "gps Latitude",
                    "type": "text",
                    "value": null,
                    "fvalue": null,
                    "description": null,
                    "ikey": null,
                    "group": "Fauna EGR",
                    "preload": false,
                    "filling": false,
                    "primary": false,
                    "created": "2019-10-01T17:13:04+00:00"
                },
                {
                    "id": 44549,
                    "name": "GPS Longitude",
                    "type": "text",
                    "value": null,
                    "fvalue": null,
                    "description": null,
                    "ikey": null,
                    "group": "Fauna EGR",
                    "preload": false,
                    "filling": false,
                    "primary": false,
                    "created": "2019-10-01T17:13:04+00:00"
                },
                {
                    "id": 44550,
                    "name": "gps Altitude",
                    "type": "text",
                    "value": null,
                    "fvalue": null,
                    "description": null,
                    "ikey": null,
                    "group": "Fauna EGR",
                    "preload": false,
                    "filling": false,
                    "primary": false,
                    "created": "2019-10-01T17:13:04+00:00"
                },
                {
                    "id": 44551,
                    "name": "gps Accuracy",
                    "type": "text",
                    "value": null,
                    "fvalue": null,
                    "description": null,
                    "ikey": null,
                    "group": "Fauna EGR",
                    "preload": false,
                    "filling": false,
                    "primary": false,
                    "created": "2019-10-01T17:13:04+00:00"
                }
            ],
            "files": [
                {
                    "id": 3298939,
                    "url_orig": "https:\/\/static.inventsys.com.br\/278\/file\/cb\/cb7c7aebac.jpg",
                    "url_low": "https:\/\/static.inventsys.com.br\/278\/thumb\/f-3298939-200x200c.jpg",
                    "type": "photo",
                    "format": "jpg",
                    "size": 1040359,
                    "orig_name": "12559f77-4c11-4fe1-8b86-ce299a8e3bb7.jpg",
                    "description": null
                }
            ],
            "nsubitems": null,
            "nevents": "0",
            "updated_at": "2019-10-01T17:13:04+00:00",
            "created_at": "2019-10-01T17:13:04+00:00",
            "deleted_at": null
        }
    ],
    "nitems": 1505,
    "current_time": "2020-02-08T00:53:42+00:00",
    "result": "ok"
}
{'Server': 'nginx/1.15.8', 'Date': 'Sat, 08 Feb 2020 00:53:42 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Vary': 'Accept-Encoding, Accept-Encoding', 'Set-Cookie': 'PHPSESSID=pj9d35fhigr6d61nfqcfd05qc6; path=/', 'Expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'Cache-Control': 'no-store, no-cache, must-revalidate, no-cache, private', 'Pragma': 'no-cache', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'Content-Encoding': 'gzip'}

如何通过循环获得所有 1505 个项目?非常感谢!

标签: pythonrestapiloopspython-requests

解决方案


由于我们不知道页数(可用projectid),您可以使用try & except如下:

projectid = 0 # initiate value for projectid
while True:
    try:
        url = 'https://api.inventsys.com.br/v4/projects/'+projectid+'/items'
        ativos = requests.request('GET', url, headers=headers, data=payload, allow_redirects=False)
        # do any thing with ativos
    except HTTPError:
        # handle HTTPError
        logging.error('HTTPError')
    # ... put any other Exception you need to handle here
    except Exception as e:
        # for handle unknown exception
        logging.error('Unknown exception')
    else:
        # loop over projectid
        projectid += 1

推荐阅读