最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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")
发布评论

评论列表(0)

  1. 暂无评论