R
R
RetAndr2020-05-06 14:27:29
Python
RetAndr, 2020-05-06 14:27:29

Python requests how to form a second request?

Good afternoon!
It is necessary to automate the work on the site.
I don’t give a link, because The site can only be accessed from the corporate network.
You need to get new users.
Fill in the user data in the form using the required link, click the add button.
After that, a table with the entered data is loaded, which must be confirmed with the "Confirm" button.
Next, we get a table with credentials and a password that needs to be parsed.
1st request: authorization + entering parameters

import requests


login = 'login'
password = 'pswd'

base_url = 'http://test.ru/?action=user_edit'

def get_html(url):
    session = requests.Session()
    res = session.get(url)
    cookies = dict(res.cookies)
#параметры для первого запроса с авторизацией и вводом данных
    params1 = {
            'login':login,
            'pass':password,
            'sp_code': '67',
            'contract_number': '111',
            'email': '[email protected]',
            'phone': '111111111',
            'phone_mobile': '11111111',
            'apply': 'Добавить'
            }

#параметры для второго запроса, подтвердить данные
    params2 = {
            'confirm': 'Подтвердить'
            }
    r = session.post(url, data = params1, cookies = cookies)   #1й запрос
    return r.text

def main():
    page = get_html(base_url)
    print(page)

if __name__ == '__main__':
    main()


2nd request: confirm the entered parameters
Actually, the question is how to set the second request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Ilyin, 2020-05-06
@sunsexsurf

why don't you want to put params1/parpams2 in list?
and then call it in a loop through this list: first element, second element

for i in params_list:
    ... для i[0]
    ... для i[1]

or in a dict (with nesting of internal dictionaries) and call with dict['params1'] the dictionary lying in value:
for paramkey in param_dict.keys():
    ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question