简体   繁体   中英

Python-requests, how to get login token

I am trying to login to a URL and get info about the products on my cart. For the for data of the login request I need username and password, and also a token and a recaptcha response that I don't know how to get them. The token has the name "struts.token" and I am trying to get it through this code, but is not working.

import requests
from bs4 import BeautifulSoup as bs

headers = {
    'user-agent' : 'your user agent'
}

with requests.Session() as s:

    s.headers = headers
    r = s.get('https://www.lidl.es/es/account/login')
    soup = bs(r.content, 'html.parser')
    token = soup.find('input', {'name' : 'struts.token'})['value']
    print(token)

When I inspect the elements and.network through my browser I can find the token easily, but at the request response I can't. Any suggestion?

Thanks in advance:)

if the token exists when you inspect elements on chrome but does not exist on the request text this means that you're request is being blocked by the website and its serving you a html page different from what your browser gets inspect the browser get request to the website you're trying to access and change the headers of the request get to those of the browser get

a lot of websites will block requests that does not contain a referrer and a origin header

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM