How can I access a .dat file from a website that needs authentication using python. I'm able to get to the to the website and can verify that that file is there but I can't download it with python. Usually I would go the website, click on the name of the file and it would download directly to my download folder.
import requests
# Connect to the gateway server
username = 'user'
password = 'pass'
# Specify the remote file to download
url = 'location/of/website'
filename = 'data_file_of_interest.dat'
r = requests.get(url, auth=(username,password),allow_redirects=True)
open(filename, 'wb')
<_io.BufferedWriter name='data_file_of_interest.dat'>
How can I access a .dat file from a website that needs authentication using python. I'm able to get to the to the website and can verify that that file is there but I can't download it with python. Usually I would go the website, click on the name of the file and it would download directly to my download folder.
import requests
# Connect to the gateway server
username = 'user'
password = 'pass'
# Specify the remote file to download
url = 'location/of/website'
filename = 'data_file_of_interest.dat'
r = requests.get(url, auth=(username,password),allow_redirects=True)
open(filename, 'wb')
<_io.BufferedWriter name='data_file_of_interest.dat'>
Share Improve this question asked Nov 20, 2024 at 15:55 uSER_23uSER_23 11 Answer
Reset to default 0After opening a blank file you have to save to it right? same: so do:
...
r = requests.get(url,allow_redirects=True)
with open(filename, 'wb') as file:
file.write(r.content)
you can make sure to catch up errors with:
...
if r.status_code == 200:
with open(filename, 'wb') as file:
file.write(r.content)
else:
print("request not succesful")