带有验证令牌的Python post请求

2024-09-30 08:37:52 发布

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

我查看了stackoverflow上的许多其他回复,并观看了一些youtube上的长视频,还没有找到可以解决我问题的信息。我曾在其他情况下使用post登录和刮取,只是在有请求令牌时没有。也许有人可以看看?我将在图片中包含post参数。应该注意的是,每次我登录时,_RequestVerificationToken都会更改

import requests

payload = {
'Email': 'dfo.msdi-idsm.mpo@dfo-mpo.gc.ca',
'Password': 'notrealpassword',
'__RequestVerificationToken': 'RuOal6rKBCWdjNy_15nTS9duFSkM586yGD3LxlWrlGzBB3Noaha9hAm3-tMq9HPvvMstDL8BZ8PvQLPutsreSeGwQOzK1MMwl76Q2c7n1zo1',
}

URL = 'http://gisd.dfo-mpo.gc.ca/DFO_WebServiceManager/Account/Login'
Pacific = 'https://gisd.dfo-mpo.gc.ca/DFO_WebServiceManager/FGP?SearchString=Pacific'

with requests.session() as c:
    c.post(URL, data=payload)
    r = c.get(Pacific)
    print r.content

enter image description here


Tags: url视频youtubestackoverflowpostrequestsgcca
1条回答
网友
1楼 · 发布于 2024-09-30 08:37:52

该令牌看起来像CSRF令牌。您应该将包含__RequestVerificaionToken的cookie添加到请求中,并查看它是否有效

import requests

payload = {
'Email': 'dfo.msdi-idsm.mpo@dfo-mpo.gc.ca',
'Password': 'notrealpassword',
'__RequestVerificationToken': 'RuOal6rKBCWdjNy_15nTS9duFSkM586yGD3LxlWrlGzBB3Noaha9hAm3-tMq9HPvvMstDL8BZ8PvQLPutsreSeGwQOzK1MMwl76Q2c7n1zo1',
}

cookies = {
'__RequestVerificationToken': 'your token',
}

URL = 'http://gisd.dfo-mpo.gc.ca/DFO_WebServiceManager/Account/Login'
Pacific = 'https://gisd.dfo-mpo.gc.ca/DFO_WebServiceManager/FGP?SearchString=Pacific'

with requests.session() as c:
    c.post(URL, data=payload, cookies=cookies)
    r = c.get(Pacific)
    print r.content

相关问题 更多 >

    热门问题