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.
Asked
Active
Viewed 181 times
0
Life is complex
- 15,374
- 5
- 29
- 58
Ramineni Ravi Teja
- 3,568
- 26
- 37
-
Are you only downloading one file or multiple files? – Life is complex May 05 '23 at 13:42
-
It is only one file – Ramineni Ravi Teja May 08 '23 at 07:17
-
Can you try the solution mentioned in this [post](https://stackoverflow.com/questions/71537664/handle-alert-with-python-and-selenium). – indayush May 10 '23 at 07: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 May 11 '23 at 14:49
-
Any updates based on my answer? – Life is complex May 12 '23 at 21:57
2 Answers
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
-
Already tried this code, but this doesn't work. this gives the issue Access Denied – Ramineni Ravi Teja May 08 '23 at 08:23
-
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
-
-
I added some additional code to determine the status of the `session.post.` What status code are you getting here? – Life is complex May 09 '23 at 12:27
-
-
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 May 10 '23 at 16:30
-
If you cannot share the URL then I need to see the headers for this `auth_connection.headers` – Life is complex May 10 '23 at 16:48
-
{'Server': 'AkamaiGHost', 'Mime-Version': '1.0', 'Content-Type': 'text/html', 'Content-Length': '260', 'Expires': 'Thu, 11 May 2023 03:00:28 GMT', 'Cache-Control': 'max-age=0, no-cache, no-store', 'Pragma': 'no-cache', 'Date': 'Thu, 11 May 2023 03:00:28 GMT', 'Xonnection': 'close', 'Server-Timing': 'cdn-cache; desc=HIT, edge; dur=1, ak_p; desc="467715_1750514755_24981958_42_44740_0_-";dur=1', 'Akamai-X-True-TTL': '-1', 'Connection': 'Keep-Alive'} – Ramineni Ravi Teja May 11 '23 at 03:04
-
Thanks. So are you getting a 403 on this line print(auth_connection.status_code)? – Life is complex May 11 '23 at 03:49
-
yes, getting a 403 on this line print(auth_connection.status_code) – Ramineni Ravi Teja May 11 '23 at 03:53
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/253596/discussion-between-life-is-complex-and-ramineni-ravi-teja). – Life is complex May 11 '23 at 04:02
