使用请求将CSRF令牌发送到表单

时间:2018-03-04 00:59:05

标签: python web-scraping python-requests osticket

我正在使用请求库登录包含以下格式的页面:

<form action="login.php" method="post" id="login">
        <input type="hidden" name="__CSRFToken__" value="022d7665c7dc20683af1a66680560500f9ad4770" />        <input type="hidden" name="do" value="scplogin">
        <fieldset>
        <input type="text" name="userid" id="name" value="" placeholder="E-mail ou Usuário"
            autofocus autocorrect="off" autocapitalize="off">
        <input type="password" name="passwd" id="pass" placeholder="Senha" autocorrect="off" autocapitalize="off">
                        <button class="submit button pull-right" type="submit" name="submit"><i class="icon-signin"></i>Login</button>
        </fieldset>
    </form>

我使用了以下代码,但我没有成功

Import requests
from bs4 import BeautifulSoup

session = requests.Session()



url = "https://page.with.form/scp/login.php"


# Retrieve the CSRF token first
soup = BeautifulSoup(client.get(url).content)
csrftoken = soup.find('input', dict(name='__CSRFToken__'))['value']
print(csrftoken)

params = {'__CSRFToken__': csrftoken, 'do': 'scplogin', 'passwd':'password', 'submit':'', 'userid':'username'}

s = session.post(url,params)
print("Cookie is set to:")
print(s.cookies.get_dict())
print("----------------")

print(s.text)

有人能帮助我吗?谢谢

0 个答案:

没有答案