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.
- Are you only downloading one file or multiple files?Life is complex– Life is complex2023-05-05 13:42:41 +00:00Commented May 5, 2023 at 13:42
- It is only one fileRamineni Ravi Teja– Ramineni Ravi Teja2023-05-08 07:17:57 +00:00Commented May 8, 2023 at 7:17
- Can you try the solution mentioned in this post.indayush– indayush2023-05-10 07:07:07 +00:00Commented 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.Life is complex– Life is complex2023-05-11 14:49:42 +00:00Commented May 11, 2023 at 14:49
- Any updates based on my answer?Life is complex– Life is complex2023-05-12 21:57:48 +00:00Commented May 12, 2023 at 21:57
Add a comment |
2 Answers
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) 2 Comments
Ramineni Ravi Teja
Already tried this code, but this doesn't work. this gives the issue Access Denied
Azam
do you know how to use specific web browser in here?
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
Ramineni Ravi Teja
Response status code 403
Life is complex
I added some additional code to determine the status of the
session.post. What status code are you getting here?Ramineni Ravi Teja
Same status code 403
Life is complex
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?Life is complex
If you cannot share the URL then I need to see the headers for this
auth_connection.headers |
