首页 > 解决方案 > 为什么我的 Python 请求模块代码无法正常工作并让我自动登录 Reddit?它不断返回 400 响应

问题描述

通过使用请求模块,我试图在我的 Reddit 帐户程序中创建一个“自动登录”。我的 Python 代码:

   s = requests.Session()
   url = 'https://www.reddit.com/login'
   payload = {'username':'username','password':'password'}
   response = s.post(url,data=payload)
   response.status_code
   response.content 

'response.status_code' 一直返回 400。当我的 Chrome 设置允许 cookie 时,为什么我一直收到 400 http 响应?此外,“response.content”返回我有一个错误的 CSRF 令牌。

b'<html>\n <head>\n  <title>400 Bad CSRF Token</title>\n </head>\n <body>\n  <h1>400 Bad CSRF Token</h1>\n  Access is denied.  This server can not verify that your cross-site request forgery token belongs to your login session.  Either you supplied the wrong cross-site request forgery token or your session no longer exists.  This may be due to session timeout or because browser is not supplying the credentials required, as can happen when the browser has cookies turned off.<br/><br/>\ncheck_csrf_token(): Invalid token\n\n\n </body>\n</html>

这完全让我感到困惑,因为我正在使用会话。我不知道为什么会出错,有人知道为什么以及如何解决这个问题吗?

标签: pythonpython-3.xpython-requestsredditpython-requests-html

解决方案


它对我有用。使用 requests.post url 作为用户个人资料链接。

import requests
from bs4 import BeautifulSoup

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0"
}
def main(url):
    with requests.Session() as req:
        req.headers.update(headers)
        response = req.get(url)
        soup = BeautifulSoup(response.content, 'lxml')
        csrf = soup.find('div',{"class": "App m-desktop"}).find(attrs={'name':'csrf_token'}).get('value')
        payload = {'username':'username','password':'password',"csrf_token":csrf}
        username = 'username'
        login_url = f"https://www.reddit.com/user/{username}"
        data = req.get(login_url, data=payload)
        print(data.content)
        print(data.status_code)

url = 'https://www.reddit.com/login/'
main(url=url)

推荐阅读