如何使用Python模块Requests传递csrftoken?这是我的代码,但它没有起作用,我不确定应该将它传递给哪个参数(data、headers、auth...)
import requests
from bs4 import BeautifulSoup
URL = 'https://portal.bitcasa.com/login'
client = requests.session(config={'verbose': sys.stderr})
# Retrieve the CSRF token first
soup = BeautifulSoup(client.get('https://portal.bitcasa.com/login').content)
csrftoken = soup.find('input', dict(name='csrfmiddlewaretoken'))['value']
login_data = dict(username=EMAIL, password=PASSWORD, csrfmiddlewaretoken=csrftoken)
r = client.post(URL, data=login_data, headers={"Referer": "foo"})
每次都是相同的错误信息。
<h1>Forbidden <span>(403)</span></h1>
<p>CSRF verification failed. Request aborted.</p>
username
、password
、csrfmiddlewaretoken
和next
吗?或者除此之外还有其他字段吗?当您将next='/'
添加到您的login_data
字典中时会发生什么? - Martijn Pietersnext='/'
设置为相同的错误。 - Jeffclient.get
但不进行解析,只需使用value = client.cookies ['csrftoken']
。否则,无法确定。 - Martijn Pieters