首页 > 解决方案 > Python - 网站登录 - 抓取 CSRF 令牌失败

问题描述

我是新来的,也是编程新手。我正在尝试登录 URL =' https://www.esselunga.it/area-utenti/applicationCheck?AppName=esselungaEcommerce&daru=https%3A%2F%2Fwww.esselungaacasa.it%3A443%2Fecommerce%2Fnav%2Fauth% 2Fsupermercato%2Fhome.html%3F&loginType=light '。

当我检查网页时,我清楚地看到了 HTML 中的 csrf_token ('name'='X-CSRF-TOKEN') 及其值;但是,当我用 BeautifulSoup 提取它时,它不可用。我检查了内容(requests.session().get(url).content),csrf_token 行消失了,在“查看页面源”中也是如此。

是否仍然可以检索 CSRF,或者无论如何都可以在没有它的情况下登录?

谢谢!


import requests                                                                       
from bs4 import BeautifulSoup                                                                                                                                               

## Here below the data collected from the Network source view, once i logged in. Form Data section:

login_data= {'username':'xxxx',  # <- instead of xxx put your username
            'password':'xxxx',   # <- instead of xxx put your psswrd               
            'daru':'xxx',        # <- data taken from the FORM DATA 
            'dare':'xxxx',       # <- data taken from the FORM DATA 
            'appName': 'xxx'}    # <- data taken from the FORM DATA 

with requests.Session() as s:                                                          
    login_url="xxx"                 #<- the URL is above in the comment
    result = s.get(login_url)    
    print (result.content)          # here you can see the X-CSRF-TOKEN is not extracted.                                                               
    soup = BeautifulSoup(result.content,'html5lib')                                         
    p = soup.find('input', attrs={'name':'X-CSRF-TOKEN'})['value']    # I can see it in the inspection tab                      
    print(p)         # csrf token is not in the content, neither when i check the 'view page source'. But in the inspection I see it!


    ## I am stuck at the above line. The following would be:                                                                                                                                                                                                                                       
    login_data['X-CSRF-TOKEN']) = p                                                   
    r = s.post(login_url,data=login_data)                                                   
    print(r.content)

标签: htmlpython-3.xmissing-datalogin-scriptcsrf-token

解决方案


推荐阅读