python - How to access a file hyperlinked in a webpage - Stack Overflow

How can I access a .dat file from a website that needs authentication using python. I'm able to ge

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 1
Add a comment  | 

1 Answer 1

Reset to default 0

After 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")

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742347040a4426750.html

相关推荐

  • python - How to access a file hyperlinked in a webpage - Stack Overflow

    How can I access a .dat file from a website that needs authentication using python. I'm able to ge

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信