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.

5
  • Are you only downloading one file or multiple files? Commented May 5, 2023 at 13:42
  • It is only one file Commented May 8, 2023 at 7:17
  • Can you try the solution mentioned in this post. Commented May 10, 2023 at 7:07
  • I added an update on how to pass headers data with the post request. I cannot tell you what these headers are, because I don't have access to the URL that you are trying to connect with. As I mentioned in our extend chat, you can get these headers by using the Development tools in Chrome. Commented May 11, 2023 at 14:49
  • Any updates based on my answer? Commented May 12, 2023 at 21:57

2 Answers 2

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) 
Sign up to request clarification or add additional context in comments.

2 Comments

Already tried this code, but this doesn't work. this gives the issue Access Denied
do you know how to use specific web browser in here?
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) 

9 Comments

Response status code 403
I added some additional code to determine the status of the session.post. What status code are you getting here?
Same status code 403
This line print(auth_connection.status_code) gives you a 403? If this is the case then your credentials aren't being accepted. Can you provide the URL for the main page?
If you cannot share the URL then I need to see the headers for this auth_connection.headers
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.