0

I have attached the link below, I use to run this link in google chrome, after that it will automatically start downloading CSV in the downloads folder with the default name "xxx.csv". I tried multiple codes to download the file and copy it to a specific location with a specific name but not achieved.

def download_files():
    
    session = requests.session()
    session.headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)"}

    for idFile in report_files:
        login = session.get(url, auth=HttpNtlmAuth('USER','PASSWORD'), verify=False)

Sample link: https://xxx.corp.xxcloud.net/exportdata/getdata?Format=CSV&QueryID=xxxx.

martineau
  • 119,623
  • 25
  • 170
  • 301

2 Answers2

0

After getting the resoonse of your get request, you need to write it to disk.

import requests

url = 'http://google.com/index.html'
r = requests.get(url, allow_redirects=True)
with open('target_path.html', 'wb') as f:
    f.write(r.content)
schilli
  • 1,700
  • 1
  • 9
  • 17
0

Try this

session = requests.session()
session.headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)"
}

for idFile in report_files:
    login = session.get(
        f"https://xxx.corp.xxcloud.net/exportdata/getdata?Format=CSV&QueryID={idFile}",
        auth=HttpNtlmAuth('USER','PASSWORD'),
        verify=False
    )
    with open(f"{idFile}.csv", "w") as f:
        f.write(login.content.decode('utf-8'))
FoxNerdSaysMoo
  • 159
  • 1
  • 1
  • 6
  • it shows an error for the above code "TypeError: write() argument must be str, not bytes" i changed it f.write(str(login.content)) and it got executed by I can see only data in 1row – Developer 2023 May 25 '21 at 14:28
  • Ok I have fixed it, you need to add `.decode('utf-8')` when writing the file – FoxNerdSaysMoo Jun 02 '21 at 22:44
  • `code`with open(f"{idFile}.csv", "w",encoding='utf-8') as f: `code` f.write(login.content.decode('utf-8')) I have done a small adjustment to your code now it working well, the only thing is giving empty rows in between each row? can you help me here? – Developer 2023 Jun 16 '21 at 16:52