0

So I am trying to download a file from and API which will be in csv format

I generate a link with user inputs and store it in a variable exportLink

import requests
#getProjectName
projectName = raw_input('ProjectName')
#getApiToken
apiToken = "mytokenishere"
#getStartDate
startDate = raw_input('Start Date')
#getStopDate
stopDate = raw_input('Stop Date')

url = "https://api.awrcloud.com/get.php?action=export_ranking&project=%s&token=%s&startDate=%s&stopDate=%s" % (projectName,apiToken,startDate,stopDate)
exportLink = requests.get(url).content
  • exportLink will store the generated link

  • which I must then call to download the csv file using another requests.get() command on exportLink

  • When I click the link it opens the download in a browser,

  • is there any way to automate this so it opens the zip and I can begin to edit the csv using python i.e removing some stuff?

pacholik
  • 8,607
  • 9
  • 43
  • 55
Harrada
  • 83
  • 2
  • 8

1 Answers1

0

If you have bytes object zipdata that you got with requests.get(url).content, you can extract file by file to another bytes object

import zipfile
import io
import csv

with zipfile.ZipFile(io.BytesIO(zipdata)) as z:
    for f in z.filelist:
        csvdata = z.read(f)

and then do something with csvdata

reader = csv.reader(io.StringIO(csvdata.decode()))
...
pacholik
  • 8,607
  • 9
  • 43
  • 55