0

I know that using the requests module in python I can download the file. If the link is in the form of:

https://mypage/filename.extension

This is relatively straight forward. However, how about the case where the form of the request is something among the lines of:

https://mypage/generate_report?export=csv

If input the url into a browser, it would prompt me to open or save the file. How do I save the file that the browser would prompt me to download?

Thanks!

EDIT: In my specific use case, the URL generates the link and then prompts to download the file. The file in question does not actually "exist" in the URL, which is why the usual methods mentions in the comments might not apply for this.

EDIT: I attempted the top 2 solutions from:

How to download image using requests

And even though it got a 200 response, i didn't get the CSV content, only html:

import requests
import shutil

page = requests.get("https://mypage/generate_report?export=csv",auth=('usr','pass'),stream=True)

print(page.status_code)

if page.status_code == 200:
    print('a')
    with open('sf.csv', 'wb') as f:
        page.raw.decode_content = True
        shutil.copyfileobj(page.raw, f)
rodrigocf
  • 1,951
  • 13
  • 39
  • 62

0 Answers0