欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python问卷星自动填写

程序员文章站 2022-03-14 21:17:04
疫情期间闲着无聊,每天都要填写问卷星来报告健康情况,于是就拿python爬虫写了一个问卷星自动填写系统,一起来看看吧_import requestsimport reimport timeimport randomclass WenJuanXing: def __init__(self, url,name,num,phone,address,map): """ :param url:要填写的问卷的url """ self....

疫情期间闲着无聊,每天都要填写问卷星来报告健康情况,于是就拿python爬虫写了一个问卷星自动填写系统,一起来看看吧_

import requests
import re
import time
import random


class WenJuanXing:
    def __init__(self, url,name,num,phone,address,map):
        """
        :param url:要填写的问卷的url
        """
        self.wj_url = url
        self.post_url = None
        self.header = None
        self.cookie = None
        self.data = None
        self.name = name
        self.num = num
        self.phone = phone
        self.address = address
        self.map = map

    def get_Tem(self, a):
        T = random.uniform(36.0, 37.0)
        T = str(format(T, '.1f'))
        if a:
            T = '父亲' + T
            return T
        else:
            return T

    def set_data(self):
        """
        这个函数中生成问卷的结果,可根据问卷结果,随机生成答案
        :return:
        """
        temp = '1$'
        a = str(time.strftime('%Y-%m-%d', time.localtime(time.time())))
        temp = temp + a + '}2$' + self.name + '}3$' + self.num + '}4$5}5$' + self.phone + '}6$'+self.address+'}7$'
        temp = temp + self.map + '}8$' + self.get_Tem(False) + '}9$2}10$}11$'
        temp = temp + self.get_Tem(True) + '}12$2'
        self.data = {
            'submitdata': temp
        }

    def set_header(self):
        """
        随机生成ip,设置X-Forwarded-For
        ip需要控制ip段,不然生成的大部分是国外的
        :return:
        """
        ip = '{}.{}.{}.{}'.format(112, random.randint(64, 68), random.randint(0, 255), random.randint(0, 255))
        self.header = {
            'X-Forwarded-For': ip,
            'Origin': 'https: // www.wjx.cn',
            'Host': 'www.wjx.cn',
            'Referer': self.wj_url,
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko\
                        ) Chrome/71.0.3578.98 Safari/537.36'
        }

    def get_ktimes(self):
        """
        随机生成一个ktimes,ktimes是构造post_url需要的参数,为一个整数
        :return:
        """
        return random.randint(15, 50)

    def get_response(self):
        """
        访问问卷网页,获取网页代码
        :return: get请求返回的response
        """
        response = requests.get(url=self.wj_url, headers=self.header)
        self.cookie = response.cookies
        return response

    def get_jqnonce(self, response):
        """
        通过正则表达式找出jqnonce,jqnonce是构造post_url需要的参数
        :param response: 访问问卷网页,返回的reaponse
        :return: 找到的jqnonce
        """
        jqnonce = re.search(r'.{8}-.{4}-.{4}-.{4}-.{12}', response.text)
        return jqnonce.group()

    def get_rn(self, response):
        """
        通过正则表达式找出rn,rn是构造post_url需要的参数
        :param response: 访问问卷网页,返回的reaponse
        :return: 找到的rn
        """
        rn = re.search(r'\d{9,10}\.\d{8}', response.text)
        return rn.group()

    def get_id(self, response):
        """
        通过正则表达式找出问卷id,问卷是构造post_url需要的参数
        :param response: 访问问卷网页,返回的reaponse
        :return: 找到的问卷id
        """
        id = re.search(r'\d{8}', response.text)
        return id.group()

    def get_jqsign(self, ktimes, jqnonce):
        """
        通过ktimes和jqnonce计算jqsign,jqsign是构造post_url需要的参数
        :param ktimes: ktimes
        :param jqnonce: jqnonce
        :return: 生成的jqsign
        """
        result = []
        b = ktimes % 10
        if b == 0:
            b = 1
        for char in list(jqnonce):
            f = ord(char) ^ b
            result.append(chr(f))
        return ''.join(result)

    def get_start_time(self, response):
        """
        通过正则表达式找出问卷starttime,问卷是构造post_url需要的参数
        :param response: 访问问卷网页,返回的reaponse
        :return: 找到的starttime
        """
        start_time = re.search(r'\d+?/\d+?/\d+?\s\d+?:\d{2}', response.text)
        return start_time.group()

    def set_post_url(self):
        """
        生成post_url
        :return:
        """
        self.set_header()  # 设置请求头,更换ip
        response = self.get_response()  # 访问问卷网页,获取response
        ktimes = self.get_ktimes()  # 获取ktimes
        jqnonce = self.get_jqnonce(response)  # 获取jqnonce
        rn = self.get_rn(response)  # 获取rn
        id = self.get_id(response)  # 获取问卷id
        jqsign = self.get_jqsign(ktimes, jqnonce)  # 生成jqsign
        start_time = self.get_start_time(response)  # 获取starttime
        time_stamp = '{}{}'.format(int(time.time()), random.randint(100, 200))  # 生成一个时间戳,最后三位为随机数
        url = 'https://www.wjx.cn/joinnew/processjq.ashx?curID={}&t={}&starttim' \
              'e={}&source=directphone&submittype=1&ktimes={}&hlv=1&rn={}&jpm=13&jqnonce={}&j'\
              'qsign={}'.format(id, time_stamp, start_time, ktimes, rn, jqnonce, jqsign)
        self.post_url = url  # 设置url
        print(self.post_url)

    def post_data(self):
        """
        发送数据给服务器
        :return: 服务器返回的结果
        """
        self.set_data()
        response = requests.post(url=self.post_url, data=self.data, headers=self.header, cookies=self.cookie)
        return response

    def run(self):
        """
        填写一次问卷
        :return:
        """
        self.set_post_url()
        result = self.post_data()
        print(result.content.decode())

    def mul_run(self, n):
        """
        填写多次问卷
        :return:
        """
        for i in range(n):
            time.sleep(1)
            self.run()


if __name__ == '__main__':
    '''
    注意:
        1.后面两个地址注意格式
        2.第二个地址后面参数为该地区经纬度,需要获取请到百度地图开发平台->解决方案->坐标拾取器中获取
    '''
    w = WenJuanXing('问卷星网址','姓名','学号','手机号','地址比如(河北-张家口市-怀来县)','地址精度比如(河北省\
    张家口市怀来县土木镇太平堡村[115.575849, 40.384272])')
    w.mul_run(3)

  

本文地址:https://blog.csdn.net/IP_yihui/article/details/110226868