0

When accessing 'https://testtest-api.com/logs' (fake website), it instantly downloads an Excel file to my computer without loading any webpage. Is there a way to download it into my python's project directory?

When I print "content", I get some kind of "bit" version of the Excel, but how can I get the Excel file in and of itself?

import requests

url = 'https://testtest-api.com/logs'
response = requests.get(url)
print(response.content)

b'Fruit, Price, ....\n ...'

Yen
  • 59
  • 5
  • `https://test-api.com/logs` is an actual website, so unless it is the site you are referring to in your question, I would avoid using it, even for example. – jabbson Jul 25 '22 at 01:06
  • Does this answer help? https://stackoverflow.com/questions/16923898/how-to-get-the-raw-content-of-a-response-in-requests-with-python, or this https://stackoverflow.com/questions/31126596/saving-response-from-requests-to-file or even this is you don't have to use requests: https://stackoverflow.com/questions/19602931/basic-http-file-downloading-and-saving-to-disk-in-python – jabbson Jul 25 '22 at 01:18

1 Answers1

1

You can use the pandas package to read an Excel file:

import pandas as pd
url = "https://test-api.com/logs"
df = pd.read_excel(url)

Check out the function's documentation for more information.

Sebastian Liendo
  • 781
  • 4
  • 12
  • Isn't pandas a bit too big and there are lighter options out there? – Yen Jul 25 '22 at 00:57
  • It depends on what you're trying to do. I'm assuming you want to do some data analysis on the excel file after downloading it. If that is the case, then pandas will be of tremendous help. If that is not your use case, then please specify what you're trying to achieve so that the answers you get are of more help. – Sebastian Liendo Jul 25 '22 at 01:05
  • In my case, I need the actual Excel file. However I edited my title to include "file" because I'd be nice to have a generic way to download any file possible that is sent from an API. – Yen Jul 25 '22 at 01:09
  • I can use urllib.request.urlretrive(response.url, 'test.csv'), however I'd like to get the file itself without having to rename it or give it an extension myself. – Yen Jul 25 '22 at 01:16
  • If i understand correctly, you can fetch the file without any problem, but don't want to give it a name or an extension? If so, just don't pass a name ('test.csv') to the urlretrieve function. Urllib will then create a temporary file with a generated name. – Sebastian Liendo Jul 25 '22 at 02:01
  • Exactly, but when I don't pass a name, for some reason it just does nothing and gives back no error message. It exists with a code 0, as if nothing happened. – Yen Jul 25 '22 at 02:02
  • But something *did* happen: a temporary file was created. Do `filename, headers = urllib.request.urlretrieve(url); print(filename)` and that will show you the path where your file is located. – Sebastian Liendo Jul 25 '22 at 02:15
  • Ok, you're right but I don't get how exactly I'm supposed to get the actual file with the original name and extension in my current folder? 'C:\\Users\\XXXX\\AppData\\Local\\Temp\\tmp6cb35evg', – Yen Jul 25 '22 at 02:19