Python API request with variable date fields

Viewed 22

I'm new to using the requests library in python and I'm calling an API with to get data with set parameters. I want to be able to get all data where the 'date' field falls under a certain year. Dates are in ISO format. Is there a way to only pull records where the date field begins with 2020 for instance?

The code below obviously doesn't work because no date fields are equal to just '2020'. A sample date could be 2020-05-12.

    params = {
    'weather':'rainy',
    'date': '2021'
    }
1 Answers

The requests library doesn't do special logic to handle dates in the request parameters. It simply sends those parameters to the API.

It's possible that the API you're calling supports the use of a date range. In that case, you could make the start of the range the moment at which 2020 started, and the end of the range the moment at which 2021 started, but without knowing what API you're calling, I can't say more.

If the API doesn't support a way to query by date range, you could try to request all the data, and then filter out the data you don't care about within your application, but that might result in quite a lot of network traffic for data that you don't care about.

Related