1 #!!!注意:修改main里的url为真实的url,按需修改 50行 set_data中的submitdata 2 # 61行 ip 修改为 真ip 3 # submittype可能有错误,在151行 4 # 提交可能出错 5 6 7 import requests 8 import re 9 import time 10 import random 11 import datetime 12 13 class WenJuanXing: 14 def __init__(self, url): 15 """ 16 :param url:要填写的问卷的url 17 """ 18 self.wj_url = url 19 self.post_url = None 20 self.header = None 21 self.cookie = None 22 self.data = None 23 24 def set_data(self): 25 """ 26 这个函数中生成问卷的结果,可根据问卷结果,随机生成答案 27 :return: 28 """ 29 now=str(datetime.date.today()) 30 31 def temperature(): 32 ''' 33 生成体温 34 :return: 35 ''' 36 high=round(random.uniform(36.5,37.0),1) 37 low=round(high-round(random.uniform(0.3,0.5),1),1) 38 high=str(high) 39 low=str(low) 40 return high,low 41 42 temperature_self_high,temperature_self_low=temperature() 43 temperature_m_high,temperature_m_low=temperature() 44 temperature_f_high,temperature_f_low=temperature() 45 temperature_s_high,temperature_s_low=temperature() 46 #temperature_g_high,temperature_g_low=temperature() 47 48 #submitdata也可以从post里复制 50 submitdata='1$'+now+'}2$班级}3$姓名}4$学号}5$1}6$无}7$'+temperature_self_high+'}8$'+temperature_self_low+'}9$地址[xxx.xxxxxx,xx.xxxxxx]}10$1|2}11$父亲姓名}12$'+temperature_f_high+'}13$'+temperature_f_low+'}14$母亲姓名}15$'+temperature_m_high+'}16$'+temperature_m_low+'}17$数量}18$关系姓名,'+temperature_s_high+'/'+temperature_s_low 51 #如果要更多的其他同住人,追加以下内容 52 # +',关系姓名,'+temperature_g_high+'/'+temperature_g_low 53 self.data = {'submitdata':submitdata} 54 55 def set_header(self): 56 """ 57 随机生成ip,设置X-Forwarded-For 58 ip需要控制ip段,不然生成的大部分是国外的 59 :return: 60 """ 62 ip = '{}.{}.{}.{}'.format(112, random.randint(64, 68), random.randint(0, 255), random.randint(0, 255)) 63 self.header = { 64 'X-Forwarded-For': ip, 65 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0', 66 } 67 68 def get_ktimes(self): 69 """ 70 随机生成一个ktimes,ktimes是构造post_url需要的参数,为一个整数 71 :return: 72 """ 73 return random.randint(15, 50) 74 75 def get_response(self): 76 """ 77 访问问卷网页,获取网页代码 78 :return: get请求返回的response 79 """ 80 response = requests.get(url=self.wj_url, headers=self.header) 81 self.cookie = response.cookies 82 return response 83 84 def get_jqnonce(self, response): 85 """ 86 通过正则表达式找出jqnonce,jqnonce是构造post_url需要的参数 87 :param response: 访问问卷网页,返回的reaponse 88 :return: 找到的jqnonce 89 """ 90 jqnonce = re.search(r'.{8}-.{4}-.{4}-.{4}-.{12}', response.text) 91 return jqnonce.group() 92 93 def get_rn(self, response): 94 """ 95 通过正则表达式找出rn,rn是构造post_url需要的参数 96 :param response: 访问问卷网页,返回的reaponse 97 :return: 找到的rn 98 """ 99 rn = re.search(r'd{9,10}.d{8}', response.text) 100 return rn.group() 101 102 def get_id(self, response): 103 """ 104 通过正则表达式找出问卷id,问卷是构造post_url需要的参数 105 :param response: 访问问卷网页,返回的reaponse 106 :return: 找到的问卷id 107 """ 108 id = re.search(r'd{8}', response.text) 109 return id.group() 110 111 def get_jqsign(self, ktimes, jqnonce): 112 """ 113 通过ktimes和jqnonce计算jqsign,jqsign是构造post_url需要的参数 114 :param ktimes: ktimes 115 :param jqnonce: jqnonce 116 :return: 生成的jqsign 117 """ 118 result = [] 119 b = ktimes % 10 120 if b == 0: 121 b = 1 122 for char in list(jqnonce): 123 f = ord(char) ^ b 124 result.append(chr(f)) 125 return ''.join(result) 126 127 def get_start_time(self, response): 128 """ 129 通过正则表达式找出问卷starttime,问卷是构造post_url需要的参数 130 :param response: 访问问卷网页,返回的reaponse 131 :return: 找到的starttime 132 """ 133 start_time = re.search(r'd+?/d+?/d+?sd+?:d{2}', response.text) 134 return start_time.group() 135 136 def set_post_url(self): 137 """ 138 生成post_url 139 :return: 140 """ 141 self.set_header() # 设置请求头,更换ip 142 response = self.get_response() # 访问问卷网页,获取response 143 ktimes = self.get_ktimes() # 获取ktimes 144 jqnonce = self.get_jqnonce(response) # 获取jqnonce 145 rn = self.get_rn(response) # 获取rn 146 id = self.get_id(response) # 获取问卷id 147 jqsign = self.get_jqsign(ktimes, jqnonce) # 生成jqsign 148 start_time = self.get_start_time(response) # 获取starttime 149 time_stamp = '{}{}'.format(int(time.time()), random.randint(100, 200)) # 生成一个时间戳,最后三位为随机数 150 151 url = 'https://www.wjx.cn/joinnew/processjq.ashx??submittype=5&curID={}&t={}&starttim' 152 'e={}&ktimes={}&rn={}&jqnonce={}&jqsign={}'.format(id, time_stamp, start_time, ktimes, rn, jqnonce, jqsign) 153 self.post_url = url # 设置url 154 print(self.post_url) 155 156 def post_data(self): 157 """ 158 发送数据给服务器 159 :return: 服务器返回的结果 160 """ 161 self.set_data() 162 response = requests.post(url=self.post_url, data=self.data, headers=self.header, cookies=self.cookie) 163 return response 164 165 def run(self): 166 """ 167 填写一次问卷 168 :return: 169 """ 170 self.set_post_url() 171 result = self.post_data() 172 print(result.content.decode()) 173 174 if __name__ == '__main__': 175 w = WenJuanXing('url') 176 w.run()
参考:
https://www.jianshu.com/p/34961ceedcb4