以某登录论坛发帖为例
方式一:导入OrderedDict模块
from collections import OrderedDict import requests import re session_req=requests.session() #打开论坛
response01=session_req.get('http://47.107.178.45/phpwind/') body=response01.content.decode('utf-8') tokenid=re.findall('name="csrf_token" value="(.+?)"/>',body)[0] print(tokenid) get_param_data={'m':'u','c':'login','a':'dologin'} form_data={ 'username':'test01', 'password':'123456', 'csrf_token':tokenid, 'csrf_token':tokenid } headerinfos={ 'Accept':'application/json, text/javascript, */*; q=0.01', 'X-Requested-With':'XMLHttpRequest', 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36', } #登录论坛
response02=session_req.post(url='http://47.107.178.45/phpwind/index.php', params=get_param_data, data=form_data, headers=headerinfos) body=response02.content.decode('utf-8') statu_id=re.findall('_statu=(.+?)"',body)[0] # print(statu_id)
#登录后的跳转
get_param_data={'m':'u', 'c':'login', 'a':'welcome', '_statu':statu_id } response03=session_req.get(url='http://47.107.178.45/phpwind/index.php', params=get_param_data) # #发帖
get_data={ 'c':'post', 'a':'doadd', '_json':'1', 'fid':'73' } form_data1=OrderedDict( [ ('atc_title',(None,'newdream123')), ('atc_content',(None,'newdream123')), ('pid',(None,'')), ('tid',(None,'')), ('special',(None,'default')), ('reply_notice',(None,'1')), ('csrf_token',(None,tokenid)) ] ) response04=session_req.post(url='http://47.107.178.45/phpwind/index.php', params=get_data, headers=headerinfos, files=form_data1) print(response04.content.decode('utf-8'))
方式二:引入multipartformdata类
import requests import re session_req=requests.session() #打开论坛
response01=session_req.get('http://47.107.178.45/phpwind/') body=response01.content.decode('utf-8') tokenid=re.findall('name="csrf_token" value="(.+?)"/>',body)[0] print(tokenid) get_param_data={'m':'u','c':'login','a':'dologin'} form_data={ 'username':'test01', 'password':'123456', 'csrf_token':tokenid, 'csrf_token':tokenid } headerinfos={ 'Accept':'application/json, text/javascript, */*; q=0.01', 'X-Requested-With':'XMLHttpRequest', 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36' } #登录论坛
response02=session_req.post(url='http://47.107.178.45/phpwind/index.php', params=get_param_data, data=form_data, headers=headerinfos) body=response02.content.decode('utf-8') statu_id=re.findall('_statu=(.+?)"',body)[0] # print(statu_id)
#登录后的跳转
get_param_data={'m':'u', 'c':'login', 'a':'welcome', '_statu':statu_id } response03=session_req.get(url='http://47.107.178.45/phpwind/index.php', params=get_param_data) # #发帖
class MultipartFormData(object): """multipart/form-data格式转化""" @staticmethod def format(data, boundary="----WebKitFormBoundary0AWyxfjzulY8IBvI", headers={}): """ form data :param: data: {"req":{"cno":"18990876","flag":"Y"},"ts":1,"sig":1,"v": 2.0} :param: boundary: "----WebKitFormBoundary0AWyxfjzulY8IBvI" :param: headers: 包含boundary的头信息;如果boundary与headers同时存在以headers为准 :return: str :rtype: str """
# 从headers中提取boundary信息
if "content-type" in headers: fd_val = str(headers["content-type"]) if "boundary" in fd_val: fd_val = fd_val.split(";")[1].strip() boundary = fd_val.split("=")[1].strip() else: raise Exception("multipart/form-data头信息错误,请检查content-type key是否包含boundary") # form-data格式定式
jion_str = '--{}\r\nContent-Disposition: form-data; name="{}"\r\n\r\n{}\r\n' end_str = "--{}--".format(boundary) args_str = ""
if not isinstance(data, dict): raise Exception('multipart/form-data参数错误,data参数应为dict类型') for key, value in data.items(): args_str = args_str + jion_str.format(boundary, key, value) args_str = args_str + end_str.format(boundary) args_str = args_str.replace("\'", "\"") return args_str form_data={ 'atc_title':'hello,world', 'atc_content':'hello,world', 'pid':'', 'tid':'', 'special':'default', 'reply_notice':'1', 'csrf_token':tokenid } get_data={ 'c':'post', 'a':'doadd', '_json':'1', 'fid':'73' } headerinfos={ 'Accept':'application/json, text/javascript, */*; q=0.01', 'X-Requested-With':'XMLHttpRequest', 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36', 'Content-Type':'multipart/form-data; boundary=----WebKitFormBoundary0AWyxfjzulY8IBvI' } m = MultipartFormData.format(data=form_data,headers=headerinfos) response04=session_req.post(url='http://47.107.178.45/phpwind/index.php', headers=headerinfos, params=get_data, data=m) print(response04.content.decode('utf-8'))