一个网站的数据是form-data形式提交的
image.png
直接post会失败,实现
def get_after_data(before_data):
headers = {
'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryuILbHAxyzWMIAXn2'
}
params = OrderedDict([
("type", (None, 'jyzs')),
("val[]", (None, 1111111)),
("val[]", (None, None)),
])
res = requests.post('http://ajuntools.hz.taeapp.com/mainIndex/getdata.html', files=params).json()
请求成功,返回结果为:
{"data":["30187443"],"code":"200","type":"jyzs"}
第二种方式,使用urllib3的encode_multipart_formdata方法
from collections import OrderedDict
from urllib3 import encode_multipart_formdata
params = OrderedDict([("username", (None, '130533193203240022', 'multipart/form-data')),
("password", (None, 'qwerqwer', 'multipart/form-data')),
('captchaId', (None, 'img_captcha_7d96b3cd-f873-4c36-8986-584952e38f20', 'multipart/form-data')),
('captchaWord', (None, 'rdh5', 'multipart/form-data')),
('_csrf', (None, '200ea95d-90e9-4789-9e0b-435a6dd8b57b', 'multipart/form-data'))])
m = encode_multipart_formdata(params, boundary='----WebKitFormBoundaryKPjN0GYtWEjAni5F')
print(m[0])
输出结果:
b'------WebKitFormBoundaryKPjN0GYtWEjAni5F\r\nContent-Disposition: form-data; name="username"\r\nContent-Type: multipart/form-data\r\n\r\n130533193203240022\r\n------WebKitFormBoundaryKPjN0GYtWEjAni5F\r\nContent-Disposition: form-data; name="password"\r\nContent-Type: multipart/form-data\r\n\r\nqwerqwer\r\n------WebKitFormBoundaryKPjN0GYtWEjAni5F\r\nContent-Disposition: form-data; name="captchaId"\r\nContent-Type: multipart/form-data\r\n\r\nimg_captcha_7d96b3cd-f873-4c36-8986-584952e38f20\r\n------WebKitFormBoundaryKPjN0GYtWEjAni5F\r\nContent-Disposition: form-data; name="captchaWord"\r\nContent-Type: multipart/form-data\r\n\r\nrdh5\r\n------WebKitFormBoundaryKPjN0GYtWEjAni5F\r\nContent-Disposition: form-data; name="_csrf"\r\nContent-Type: multipart/form-data\r\n\r\n200ea95d-90e9-4789-9e0b-435a6dd8b57b\r\n------WebKitFormBoundaryKPjN0GYtWEjAni5F--\r\n'
第二种方法较为完善,可以自由添加分隔符
网友评论