request 借用浏览器的 cookie 模拟POST
# -*- coding: utf-8 -*- import requests import json import time # 利用浏览器的cookie,并提交 # 需要在浏览器中添加一个学生,同时抓取到下面所需的 token 与 Cookie 值 url = 'http://www.liaoningxinhuajiaoyu.com/portal/api/user/addActivityMember.do' headers = { 'Content-Type': 'application/json', 'Referer': 'http://www.liaoningxinhuajiaoyu.com/portal/sites/site-102/pages/uploadproduction.html?docId=1396339', 'token':'38ffdab784af4916a89d47edd358bc57', 'Cookie': 'JSESSIONID=CE9D3FA7F9B22783212898DB1DCB3659; Hm_lvt_ab4237aeb81f3845de913a399f84ce4f=1556072138,1556072250; Hm_lpvt_ab4237aeb81f3845de913a399f84ce4f=1556073196' } #构造json数据,gender: 0女/1男 data = { "createTime":"", "gender":"1", "id":0, "identifyId":"210381200608223510", "mobileNum":"15941269095", "userId":453594, "userName":"王骏骐" } session = requests.Session() f = open('c:/stu.txt', 'r') for aLine in f.readlines(): # 读入的每一行要使用 strip() 方法去除行尾的\n,否则将会影响到对其中字符串的解析 items = aLine.strip().split(',') name = items[0] id = items[1] sex = items[2] if items[2] == '女': sex = 0 else: sex = 1 phone = items[3] str = '{' + '"createTime":"","gender":"{}","id":"0","identifyId":"{}","mobileNum":"{}","userId":453594,"userName":"{}"'.format(sex, id, phone, name) + '}' response = requests.post(url, headers=headers, json=json.loads(str)) print(str) time.sleep(2) f.close()
stu.txt 的内容如下:
孙悟空,210381200801021223,女,13579246810
猪八戒,210381200801031234,男,13579246811
沙和尚,210381200801041235,男,13579246812

浙公网安备 33010602011771号