简体   繁体   English

为什么我无法使用请求 Python 登录?

[英]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: Python代码:

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. “action”属性中的地址并不总是引用处理程序脚本。 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.在我的例子中,地址是“ /ajax/auth_user.php 。除了你的参数之外,你还需要看看传递了哪些参数。

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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