简体   繁体   中英

Why can't I log in using requests Python?

I can't log in to parse user data. The form tag looks like:

<form name="form_auth" class="authorization-form" method="post" target="_top" action="/personal/?login=yes">

Python code:

import requests
from bs4 import BeautifulSoup

def auth(login, password):

    url = "https://fix-price.ru/personal/"
    param = {'login': login, 'password': password}
    r = requests.post(url, data=param)
    page = BeautifulSoup(r.text, 'lxml')

    return page


page = auth('some_mail@gmail.com', 'some_password')

about_user = page.find('div', class_='header-right')

why it doesn't work? Thanks in advance.

And so, I solved the problem as follows. The address in the "action" attribute does not always refer to the handler script. It is necessary to look in the browser, on what script there is a request after pressing submit. In my case, the address was " /ajax/auth_user.php . You also need to look at what parameters are passed in addition to yours.

The working code is:

import requests
from bs4 import BeautifulSoup

def auth(login, password):
    url = "https://fix-price.ru/ajax/auth_user.php"
    param = {
        'AUTH_FORM':'Y',
        'TYPE':'AUTH',
        'backurl':'/personal/',
        'login': login,
        'password': password
    }
    r = requests.post(url, data=param)
    page = BeautifulSoup(r.text, 'lxml')

    return page


page = auth('some_mail@gmail.com', 'some_password')

about_user = page.find('div', class_='header-right')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM