0

I pulled json data with api. Response example as below.

{
  "page": 1,
  "pages": 5,
  "limit": 100,
  "count": 466,
  "records": [
    {
      "epoch": "9b4WLoXXYgga9mGRLGsWKu",
      "last_height": 5402,
      "last_time": "2021-02-22T01:02:43.011692Z",
      "expected_blocks": 432,
      "produced_blocks": 432,
    }
  ]
}

But page is limited with 100 rows. Parameters query is available for pages. Below is python code I use:

import requests
import json
import pandas as pd

response = requests.get(url, params = {'page': 1})
json_data = json.loads(response.text)
df = pd.DataFrame(json_data['records']) 
print(df)

How can I get all pages with looping?

Thanks

1 Answer 1

1

You can try this and get all your data in dataframe by looping.

import requests
import json
import pandas as pd

response = requests.get(url, params = {'page': 1}) #---> remember to keep your url 
records = []
for page_number in range(2, response.json().get("pages")+1):
    records += response.json().get('records')    
    response = requests.get(url, params = {'page': page_number})
df = pd.DataFrame(records) 
print(df)
Sign up to request clarification or add additional context in comments.

4 Comments

json_data is not defined in your solution. But when I print "records", it didn't work out as expected.
oh, sorry, try now.
@Divya_Prakash I didn't pay attention that first page of the 'records' wasn't print in df. I have changed for page_number in range(1 , ......), so now, it includes first page also. Maybe you want to edit the solution.
thanks for flagging this up, I have updated the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.