Inquire about call API python loop next parameter pages 1000 limit.
The response got a 1000 limit, how do I keep the next loop going until null?
There are also non null data but I found null because I can't loop the next parameter pages please guide me I want to do it
import requests import json def call_api(): url = "https://api.api.com/api/v2/intelligence/?&q=((status='status') and (confidence>=confidence) and (country='country'))" api_key ="apikey" user = "user" headers = { "Content-Type": "application/json", "Authorization": "apikey " user ":" api_key, } response = requests.get(url, headers=headers, verify=False) data = response.json() print(json.dumps(data, indent=4)) with open('test11.json', 'w') as json_file: json.dump(data, json_file ,indent=4) if (response.status_code == 200): print("status_code = " str(response.status_code) " The request was a success!") elif (response.status_code == 404): print("status_code = " str(response.status_code) " Result not found!") return data while True: call_api() break response data "meta": { "total_count": 170965826, "offset": 0, "limit": 1000, "took": 259, "next": null (The response got a 1000 limit, how do I keep the next loop going until null? and There are also non null data but i found null because i can't loop next parameter pages please guide me i want to do it) } } status_code = 200 The request was a success!
CodePudding user response:
Without more information, it isn't perfectly clear how your api pagination works or the potential values in next
, but this should help get you a bit further.
import requests
import json
def call_api():
url = "https://api.api.com/api/v2/more"
api_key = "apikey"
user = "user"
headers = {
"Content-Type": "application/json",
"Authorization": f"apikey {user}:{api_key}",
}
response = requests.get(url, headers=headers, verify=False)
if response.status_code != 200:
return {} # or some such handling
return response.json()
results = []
while True:
result = call_api()
if result.get("meta", {}).get("next") is None:
break
results.append(result) # or however you prefer to assemble the results
Here, we get the result from a call, then check to see if there was a None
result which we give be default if the keys aren't present. In that case, we break the loop, otherwise we call the api again until that is the case, each time appending to the results
list, but you could do that many ways.
CodePudding user response:
I have followed the instructions, I found that the run code has no data.
With the API limit 1000 timestamp and using while True to keep it running. until a null value is found and I run the code But no information is provided. Please, I want to accomplish this. Thanks
import requests import json def call_api(): url = "https://api.api.com/api/v2/intelligence/?&q=((status='status') and (confidence>=confidence) and (country='country'))" api_key ="apikey" user = "user" headers = { "Content-Type": "application/json", "Authorization": f"apikey {user}:{api_key}" } response = requests.get(url, headers=headers, verify=False) if response.status_code != 200: return {} return response.json() results = [] while True: result = call_api() if result.get("meta", {}).get("next") is None: break results.append(result) # results1 = results.json() # print(results1)
no data C:\Users\User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'api.threatstream.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings warnings.warn( PS C:\xampp\htdocs\Learning Dev Tor v.6>