Python: How to loop next parameter pages

116 Views Asked by At
  • How to loop next parameter pages

  • How to bring this value to the next loop parameter? =>> "next": "/api/v2/intelligence/?q=%28%28status%3D%27active%27%29+and+%28confidence+%3E%3D+100%29+and+%28country+%3D+TH%29+or+%28country+%3D+AD%29%29&search_after=1466972229598%2C288044306&limit=1000"

    import requests
    import json
    
    host='https://api.threatstream.com'
    api_key ="apikey"
    user = "[email protected]"
    
    def get_data(url):
    headers = {
     "Content-Type": "application/json",
     "Authorization": "apikey " + user + ":" + api_key
    }
    
    final_url= host + url
    response = requests.get(final_url, verify=False, headers=headers)
    
    data = response.json()
    print(json.dumps(data, indent=4))
    
    if (response.status_code == 200):
       print("status_code = " + str(response.status_code) + " The request was a success!")
       if data.get('meta',{}).get(next) :
         get_data(data.meta.next)
    
    elif (response.status_code == 404):
         print("status_code = " + str(response.status_code) + " Result not found!")
    
    url = "/api/v2/intelligence/?&q=((status='active') and (confidence >= 100) and (country = TH) or (country = AD))"
    
    get_data(url)
    
  • Response data

    "meta": {
     "total_count": 1154,
     "offset": 0,
     "limit": 1000,
     "took": 185,
     # How to bring this value to the next loop parameter?   
     "next": "/api/v2/intelligence/?q=%28%28status%3D%27active%27%29+and+%28confidence+%3E%3D+100%29+and+%28country+%3D+TH%29+or+%28country+%3D+AD%29%29&search_after=1466972229598%2C288044306&limit=1000" 
    
     }
     status_code = 200 The request was a success!
    
1

There are 1 best solutions below

14
On

You can recursively call the function to fetch data, if the response code is 200 and response json has next url .

import requests
import json

host='https://api.api.com'
api_key ="apikey"
user = "[email protected]"

def get_data(url):
    headers = {
        "Content-Type": "application/json",
        "Authorization": "apikey " + user + ":" + api_key
    }
    
    final_url= host + url
    response = requests.get(final_url, verify=False, headers=headers)

    data = response.json()
    print(json.dumps(data, indent=4))

    if (response.status_code == 200):
        print("status_code = " + str(response.status_code) + " The request was a success!")
        if data.meta.next:
            get_data(data.meta.next)

    elif (response.status_code == 404):
        print("status_code = " + str(response.status_code) + " Result not found!")
        
url = "/api/v2/intelligence/?&q=((status='status') and (confidence >= 'confidence') and (country = 'country'))"

get_data(url)