如何使用带CSRF令牌的robobrowser登录?

2024-09-28 13:40:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在尝试登录

https://customersso1.fortinet.com/saml-idp/login/?next=

在登录后,头指向https://support.fortinet.com/Information/ProductLifeCycle.aspx

刮除其中的所有表格

我尝试了各种方法,并确定robobrowser是理想的选择。但是,该网站有CSRF中间件令牌,不允许我登录

此外,Robobrowser中的get_form()函数不会获取用户名和密码字段供我登录。(登录页面中只有一个表单)我已经提供了凭据作为其公共信息,任何人都可以创建一个ID来访问该信息

import re
from robobrowser import RoboBrowser
from bs4 import BeautifulSoup

br = RoboBrowser()
br.open('https://customersso1.fortinet.com/saml-idp/login/?next=')

form = br.get_form()
form["id_username"] = "csmcoe.global@global.ntt"
form["id_password"] = 'Csmcoe@123'
#form["csrffieldname"] = 'X2fOGlgm0lBH60k2SqPiJ5BkFv7lRzKo' 
br.submit_form(form)

当您解析表单时

form.parsed

你得到(当你检查网页时,你会注意到表单中的div容器,但当你刮去它时,你不会注意到。因为没有用户名元素,我无法通过表单登录)

<form action="" autocomplete="off" id="login_form" method="post">
<input name="csrfmiddlewaretoken" type="hidden" value="tzDXaug8hGlNO7h6oDDU3tel80mdA9AZ"/>
<input name="next" type="hidden" value="/"/>
<div class="container" id="container"></div>
</form>

此外,除robobrowser之外的任何其他方式也值得赞赏。谢谢大家!


Tags: httpsbrimportdivrobobrowserformcomid
1条回答
网友
1楼 · 发布于 2024-09-28 13:40:44

试试这个

from simplified_scrapy import SimplifiedDoc, utils, req

# Get csrfmiddlewaretoken
url = "https://customersso1.fortinet.com/saml-idp/login/?next="
html = req.get(url)
doc = SimplifiedDoc(html)
csrfmiddlewaretoken = doc.select(
    'form#login_form>input@name=csrfmiddlewaretoken>value()')
    
username = 'your user name'
password = 'your password'
# login
html = req.post(url,
         'csrfmiddlewaretoken=' + csrfmiddlewaretoken + '&next=&username' +
         username + '&password=' + password,
         header={'Referer': url})
print (html) # See what the problem is

# Get next page
html = req.get(
    'https://support.fortinet.com/Information/ProductLifeCycle.aspx')
doc = SimplifiedDoc(html)
print(doc.title.text)

相关问题 更多 >

    热门问题