0

I am trying to download file from a website which prompts for alert login popup for entering username and password,once login file will get downloaded automatically. I want to automate this process. But the examples i am getting using Selenium examples only. I want to do this without selenium.

Life is complex
  • 15,374
  • 5
  • 29
  • 58
Ramineni Ravi Teja
  • 3,568
  • 26
  • 37

2 Answers2

1

Try using requests in Python Install

import requests

# set up the authentication credentials
username = "user"
password = "pass"
auth = (username, password)
url = "https://samp.com"
# send an HTTP GET request with the authentication credentials
response = requests.get(url, auth=auth)
with open("file_to_save.pdf", "wb") as f:
f.write(response.content)
Abhay Chaudhary
  • 1,763
  • 1
  • 8
  • 13
1

UPDATED 05.11.2023 at 2:50PM GMT

It might be possible to use requests.Session to pass your authentication credentials and then request the file.

You will likely need to pass in the headers with requests.Session.post.

This question is difficult to troubleshoot without the URL and the credentials, so I can only provide you guidance on how I would tackle this use case.

from requests import Session

# you need to look for these in your browser at I mentioned in the 
# our chat session. You likely need to pass these in with your post request
# passing these might prevent the 403 errors.

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
}


with Session() as session:
    session.headers.update(headers)
    session.auth = ("username", "password")
    auth_connection = session.post('https://somewebsite.com')
    print(auth_connection.status_code)
    if auth_connection.status_code == 200:
        response = session.get('https://somewebsite.com/datafile.csv')
        if response.status_code == 200:
            with open('/path_to_directory_you_want_to_save/file_name.text', 'wb') as f:
                f.write(response.content)
Life is complex
  • 15,374
  • 5
  • 29
  • 58