admin管理员组文章数量:1333210
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")
本文标签: pythonHow to access a file hyperlinked in a webpageStack Overflow
版权声明:本文标题:python - How to access a file hyperlinked in a webpage - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742347039a2457728.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论