Python:捕获 CSRF 令牌然后发出请求

时间:2021-01-18 08:08:43

标签: python beautifulsoup

以下网站使用 CSRF 令牌进行搜索验证,搜索端点 /app/back/search 返回 Application/JSON

我想用python做请求,我试过:

import requests                                                                
from bs4 import BeautifulSoup                                        
                                                                               
# need to capture a valid csrf token                                                                            
s = requests.session()                                                         
response = s.get('https://w.com/')                              
soup = BeautifulSoup(response.text, 'lxml')  
tokenizer = soup.find("meta", {"name": "csrf-token"}).attrs['content']  

q = "Outside the w"     
#https://w.com/app/back/search/?header=fccd7e63ec5fab6cca54ad5066ac59f3d9f644db982bd8068815155b7ad3a671-1610956435~691502883&type=kurdfilm&q=hobbit                                                     
res = s.get('https://w.com/app/back/search/?header={0}&type=kurdflim&q={1}'.format(tokenizer,q))                             
                                                                               
                           
print(res.text)

但是,使用此代码没有成功,因为它总是返回重定向主页的 html。 有什么问题?

0 个答案:

没有答案