Python code to authenticate to website, navigate through links and download files

2024/10/5 14:17:55

I'm looking something which could be interesting to you as well. I'm developing a feature using Python, which should be able to authenticate (using userid/password and/or with other preferred authentication methods) and connect to specify website, navigate through the website and download the file under a specific option. Later I have to write the schedules on developed code and automate it.

Did anyone come across such scenario and developed the code in python? Please suggest if any python libraries are there.

What I have achieved right now is:

I can download file with specific URL.

I know how to authenticate and download the file.

I'm able to pull the links from the specific website.

This is something we could achieve using selenium, but I want to write in Python.

Answer

After 5 days of research, I found what I wanted. Your urlLogin and urlAuth could be same, its totally depends on what action taken on Login button or form action. I used crome inspect option to findout the actual GET or POST request used on the portal.

Here is the answer of my own question-->

import requestsurlLogin = 'https://example.com/jsp/login.jsp'
urlAuth = 'https://example.com/CheckLoginServlet'
urlBd = 'https://example.com/jsp/batchdownload.jsp'
payload = {"username": "username","password": "password"
}# Session will be closed at the end of with block
with requests.Session() as s:s.get(urlLogin)headers = s.cookies.get_dict()print(f"Session cookies {headers}")r1 = s.post(urlAuth, data=payload, headers=headers)print(f'MainFrame text:::: {r1.status_code}')  #200r2 = s.post(urlBd, data=payload)print(f'MainFrame text:::: {r2.status_code}')  #200print(f'MainFrame text:::: {r2.text}')  #page source# 3. Again cookies will be used through session to access batch download pager2 = s.post(config['access-url'])print(f'Batch Download status:::: {r2.status_code}')  #200source_code = r2.text# print(f'Batch Download source:::: {source_code}')
https://en.xdnf.cn/q/120420.html

Related Q&A

How to get sum of products of all combinations in an array in Python? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

How to get value from entry (Tkinter), use it in formula and print the result it in label

When using the function entry of Tkinter, you can write a string value and do things with it; but Im actually working with formulas. The idea is fairly simple: to put a bunch of boxes to fill with numb…

Printing tuples in a list

Im not getting how to proceed with this problem in lists,can any one help me out? Thanks in advance.input is :l = [(1,2),(3,4),(5,6)]output is:[(1,3,5),(2,4,6)]

Adding entries from multiple files in python

I have a question on how to add entries from 100 files (each file contains two columns) and then writing them to a new file(which will also contain two columns)?

Multiply list in list string items with list in list integers

I have 2 lists: list1 and list2 list1 is a list in list and contains various strings list2 is also a list in list and consists of different integer values the dimensions of both lists is the same how d…

I want to complete the quiz with variables or lists without Python input

import random number = random.randint(1, 10)player_name = input("Hello, Whats your name?") number_of_guesses = 0 print(okay! + player_name+ I am Guessing a number between 1 and 10:)while nu…

Comparing a list to a a list of tuples?

I have two lists. One is simply a list of idsids = [123, 124, 127, 316, 463]and the other is a list of tuples of ids and namescombined = [(123, "Brian"), (124,"Eric"), (222,"Ja…

Sum from 1 to n, 2 to n, ... n in python

I was trying to get a series of sum from 1 to n, 2 to n, ..., and nFor example, if n=5, then the result should be 15 14 12 9 5Please comment for the code below. I cant figure out whats wrong.n=int(inpu…

How do I scrape the about section of a Facebook page? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…

How can I dynamically create a name of a variable in Python? [duplicate]

This question already has answers here:How do I create variable variables?(18 answers)Closed 6 years ago.I am having a function in pythondef func(dataFrame,country,sex):varible_name=dataFrame[(dataFra…