Python 301 POST

时间:2016-03-09 00:33:00

标签: python redirect post python-requests csrf

所以基本上我试图向这个网站提出请求 - https://panel.talonro.com/login/应该是301 redirect

我按照自己的意愿发送数据,但最后我的请求中没有位置标头,状态代码为200而不是301

我无法弄清楚我做错了什么。请帮忙

def do_request():
    req = requests.get('https://panel.talonro.com/login/').text
    soup = BeautifulSoup(req, 'html.parser')
    csrf = soup.find('input', {'name':'csrfKey'}).get('value')
    ref = soup.find('input', {'name':'ref'}).get('value')
    post_data = {
        'auth':'mylogin',
        'password':'mypassword',
        'login__standard_submitted':'1',
        'csrfKey':csrf,
        'ref':ref,
        'submit':'Go'
}

    post = requests.post(url = 'https://forum.talonro.com/login/', data = post_data, headers = {'referer':'https://panel.talonro.com/login/'})

1 个答案:

答案 0 :(得分:0)

现在push_data位于do_request(),因此您无法在该功能之外访问它。

相反,请尝试返回该信息,然后将其传递给:

import requests
from bs4 import BeautifulSoup

def do_request():
    req = requests.get('https://panel.talonro.com/login/').text
    soup = BeautifulSoup(req, 'html.parser')
    csrf = soup.find('input', {'name':'csrfKey'}).get('value')
    ref = soup.find('input', {'name':'ref'}).get('value')
    post_data = {
        'auth':'mylogin',
        'password':'mypassword',
        'login__standard_submitted':'1',
        'csrfKey':csrf,
        'ref':ref,
        'submit':'Go'
        }
    return post_data

post = requests.post(url = 'https://forum.talonro.com/login/', data = do_request(), headers = {'referer':'https://panel.talonro.com/login/'})