无法使用 Python 请求会话模块登录网站 [英] Can't log in to website with Python requests session module

查看:45
本文介绍了无法使用 Python 请求会话模块登录网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用网络抓取.对于我的第一个项目,我尝试使用 requests.Session() 登录到 artofproblemsolving.com 并访问另一个用户的帐户.这是我的代码:

I am just starting out with web scraping. For my first project, I'm trying to log into artofproblemsolving.com using requests.Session() and access another user's account. Here is my code:

import requests

LOGIN_URL = 'https://www.artofproblemsolving.com/Forum/ucp.php?mode=login'
DATA_URL = 'https://www.artofproblemsolving.com/Forum/memberlist.php?mode=viewprofile&u=90586'

payload = {
    'username': '{{my_username}}',
    'password': '{{my_password}}'
}

with requests.Session() as s:
    s.post(LOGIN_URL, data=payload)
    r = s.get(DATA_URL)
    print r.text

但是当我在终端中运行它时,输出的 HTML 来自登录页面,而不是我试图获取的用户个人资料.我仔细检查了 LOGIN_URL 是登录表单中的 POST 操作,用户名"和密码"是表单项的名称,并且我的登录信息是正确的.

But when I run this in terminal, the output HTML is from the login page, not the user's profile that I'm trying to grab. I double-checked that LOGIN_URL is the POST action in the login form, that 'username' and 'password' are the names of the form items, and that my login information is correct.

感谢所有帮助,谢谢!

推荐答案

我不确定这是问题的直接原因.但是表单发送到登录操作还有另一个参数.

I'm not sure this is direct reason for the problem. But there are another parameter that the form send to login action.

除了usernamepassword,还要传递以下参数:

Beside username, password, also pass following parameters:

'username': username,
'password': password,
'login': 'Login'.
'sid': sid,   # You need to parse the login page to get sid
'redirect': 'index.php',

这篇关于无法使用 Python 请求会话模块登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆