0

I'm getting the data from API using requests library doing like this:

import requests

url = "Some String"

headers = {

'Authorization':"Some Token"}

response = requests.request("GET", url, headers=headers)

But the file that I'm trying to get is very large so I receive the time exceptions error. How can I get it using chunks in request?

Thanks!

5
  • Possible duplicate of How to download large file in python with requests.py? Commented Jun 22, 2018 at 12:45
  • no, it is not :) Commented Jun 22, 2018 at 12:46
  • Do those answers not help your problem? Commented Jun 22, 2018 at 12:49
  • no, they do not Commented Jun 22, 2018 at 13:04
  • have you seen this SO post? i think using stream=True argument might help. Commented Jun 22, 2018 at 13:55

1 Answer 1

0
import requests    
import datetime
import pandas as pd


url = "some URL"
headers = {

'Authorization':"Some Token"}

start_date = datetime.datetime(2018, 6, 1)
end_date = datetime.datetime.now()
temp_end_date = start_date + datetime.timedelta(days=7)

output = dict()
while temp_end_date <= end_date:
    temp_url = url % (start_date.timestamp()*1000, temp_end_date.timestamp()*1000)
    response = requests.get(temp_url, headers=headers)
    temp_data = response.json()
    for key, value in temp_data.items():
        output_arr = output.get(key, [])
        output_arr.extend(value)
        output[key] = output_arr
    start_date = temp_end_date + datetime.timedelta(seconds=1)
    temp_end_date += datetime.timedelta(days=7)

data=output
df=pd.DataFrame(data)

df.head()
Sign up to request clarification or add additional context in comments.

Comments

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.