如何使用python请求传递csrftoken
作者:Yicsr
这篇文章主要介绍了如何使用python请求传递csrftoken问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
python请求传递csrftoken
如何通过python模块Requests传递csrftoken?
参考代码
import requests from bs4 import BeautifulSoup as bs import lxml # Page header head= { 'Content-Type':'application/x-www-form-urlencoded', 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36' } # Start Page url = 'https://www.die-staemme.de/' # Login URL login_url = 'https://www.die-staemme.de/page/auth' # URL behind the login page url2= 'https://de159.die-staemme.de/game.php?screen=overview&intro' # Open up a session s = requests.session() # Open the login page r = s.get(url) # Get the csrf-token from meta tag soup = bs(r.text,'lxml') csrf_token = soup.select_one('meta[name="csrf-token"]')['content'] # Get the page cookie cookie = r.cookies # Set CSRF-Token head['X-CSRF-Token'] = csrf_token head['X-Requested-With'] = 'XMLHttpRequest' # Build the login payload payload = { 'username': '', #<-- your username 'password': '', #<-- your password 'remember':'1' } # Try to login to the page r = s.post(login_url, cookies=cookie, data=payload, headers=head) # Try to get a page behind the login page r = s.get(url2) # Check if login was successful, if so there have to be an element with the id menu_row2 soup = bs(r.text, 'lxml') element = soup.select('#menu_row2') print(element)
import sys import requests URL = 'https://portal.bitcasa.com/login' client = requests.session() # Retrieve the CSRF token first client.get(URL) # sets cookie if 'csrftoken' in client.cookies: # Django 1.6 and up csrftoken = client.cookies['csrftoken'] else: # older versions csrftoken = client.cookies['csrf'] login_data = dict(username=EMAIL, password=PASSWORD, csrfmiddlewaretoken=csrftoken, next='/') r = client.post(URL, data=login_data, headers=dict(Referer=URL))
import sys import django from django.middleware.csrf import CsrfViewMiddleware, get_token from django.test import Client django.setup() csrf_client = Client(enforce_csrf_checks=True) URL = 'http://127.0.0.1/auth/login' EMAIL= 'test-user@test.com' PASSWORD= 'XXXX' # Retrieve the CSRF token first csrf_client.get(URL) # sets cookie csrftoken = csrf_client.cookies['csrftoken'] login_data = dict(username=EMAIL, password=PASSWORD, csrfmiddlewaretoken=csrftoken.value, next='/') r = csrf_client.post(URL, data=login_data, headers=dict(Referer=URL))
import requests from bs4 import BeautifulSoup as bs import lxml # Page header head= { 'Content-Type':'application/x-www-form-urlencoded', 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36' } # Start Page url = 'https://www.die-staemme.de/' # Login URL login_url = 'https://www.die-staemme.de/page/auth' # URL behind the login page url2= 'https://de159.die-staemme.de/game.php?screen=overview&intro' # Open up a session s = requests.session() # Open the login page r = s.get(url) # Get the csrf-token from meta tag soup = bs(r.text,'lxml') csrf_token = soup.select_one('meta[name="csrf-token"]')['content'] # Get the page cookie cookie = r.cookies # Set CSRF-Token head['X-CSRF-Token'] = csrf_token head['X-Requested-With'] = 'XMLHttpRequest' # Build the login payload payload = { 'username': '', #<-- your username 'password': '', #<-- your password 'remember':'1' } # Try to login to the page r = s.post(login_url, cookies=cookie, data=payload, headers=head) # Try to get a page behind the login page r = s.get(url2) # Check if login was successful, if so there have to be an element with the id menu_row2 soup = bs(r.text, 'lxml') element = soup.select('#menu_row2') print(element)
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。