使用Python请求登录论坛

Car*_*eta 3 python python-3.x python-requests

我正在尝试使用python请求登录论坛.这是我试图登录的论坛:http://fans.heat.nba.com/community/

这是我的代码:

import requests
import sys

URL = "http://fans.heat.nba.com/community/index.php?app=core&module=global&section=login"

def main():
    session = requests.Session()

    # This is the form data that the page sends when logging in
    login_data = {
        'ips_username': 'username',
        'ips_password': 'password',
        'signin_options': 'submit',
        'redirect':'index.php?'
    }

    r = session.post(URL, data=login_data)

    # Try accessing a page that requires you to be logged in
    q = session.get('http://fans.heat.nba.com/community/index.php?app=members&module=messaging&section=view&do=showConversation&topicID=4314&st=20#msg26627')
    print(session.cookies)
    print(r.status_code)
    print(q.status_code)

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

URL是论坛上的登录页面.使用'q'变量,会话尝试访问论坛(私人信使)上的某个网页,只有在您登录时才能访问该网页.但是,该请求的状态代码返回'403',这意味着我无法成功登录.

为什么我无法登录?在'login_data'中,'ips_username'和'ips_password'是HTML表单.但是,我相信我有实际的登录命令('signin_options','redirect')错误.

有人可以指导我正确的登录命令吗?

Cha*_*ker 5

表单中有隐藏的输入 auth_key

<input type='hidden' name='auth_key' value='880ea6a14ea49e853634fbdc5015a024' />
Run Code Online (Sandbox Code Playgroud)

所以你需要解析它并将其传递给登录页面.你可以简单地使用正则表达式

def main():
      session = requests.Session()

      # Get the source page that contain the auth_key
      r = requests.get("http://fans.heat.nba.com/community/index.php?app=core&module=global&section=login")
      # Parse it
      auth_key = re.findall("auth_key' value='(.*?)'",r.text)[0]


      # This is the form data that the page sends when logging in
      login_data = {
           'ips_username': 'username',
           'ips_password': 'password',
           'auth_key' : auth_key                                                                                                                      

      }
Run Code Online (Sandbox Code Playgroud)

其余的应该是一样的.