Selenium代刷问卷星问卷,升级版(0基础也可以自己刷)。

版权声明:多多交流。 https://blog.csdn.net/qq_42776455/article/details/82776396

前言。

第一个版本做出来的时候只是我自己用的demo,没想到会有这么多的同学都需要。所以就做了这个更方便,而且比上个版本来说最大的特点是,不同的电脑都可以执行。(上个版本因为要手动定位验证码的位置,不同电脑不一样,这才做的不用手动调试。)
因为这是比上一个版本更加优化,就没有了刷的过程,只有刷的结果。刷的过程效果,请参见:https://blog.csdn.net/qq_42776455/article/details/82151732

先刷个50份试试:
在这里插入图片描述在这里插入图片描述

环境

  • python3.6.5,python3就可以只要对应的库安装成功就好,百度搜索python3安装教程。
  • pycharm编辑器(有一定经验的可以不用就相当于执行个脚本)下载软件之后对应需要的库安装比较方便。需要的库只需要点击 + ,在弹出的框里搜索对应的库,安装就OK啦。
    在这里插入图片描述
  • 需要安装的库,selenium,PIL,requests
  • PhantomJS,相当于一个没有界面的浏览器。安装教程:https://blog.csdn.net/qq_26718271/article/details/55046198 记得要加到环境变量里。
  • 超级鹰:http://www.chaojiying.com/ 验证码识别API。这个平台还是比较好的,便宜虽然有时候极个别的会出错。可以忽略。
    在这里插入图片描述
    也就是说你一元钱,可以刷100次,我感觉挺赚的。
    先在该平台注册一个账号(账号密码记着,等下要用到。),充值个2元钱。
    打开个人中心。在这里插入图片描述

代码:

主程序,wenjuanxing.py

注意第130行,要改成你对应的超级鹰信息。

# @Author  :Z1ber
# @Version :Python3.6
# @FileName:wenjuan.py
# @Software:PyCharm
# @DateTime:2018/9/19 16:38

import time
import random
from PIL import Image
from chaojiying import Chaojiying_Client
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def autoSelect():
    driver.get('https://www.wjx.cn/m/27168497.aspx')

    xpath1 = '//*[@id="div1"]/div[2]/div[%s]' % str(random.randint(1, 2))
    answer_1 = driver.find_elements_by_xpath(xpath1)[0]
    answer_1.click()

    xpath2 = '//*[@id="div2"]/div[2]/div[%s]' % str(random.randint(1, 3))
    answer_2 = driver.find_elements_by_xpath(xpath2)[0]
    answer_2.click()

    xpath3 = '//*[@id="div3"]/div[2]/div[%s]' % str(random.randint(1, 2))
    answer_3 = driver.find_elements_by_xpath(xpath3)[0]
    answer_3.click()

    xpath4 = '//*[@id="div4"]/div[2]/div[%s]' % str(random.randint(1, 2))
    answer_4 = driver.find_elements_by_xpath(xpath4)[0]
    answer_4.click()

    xpath5 = '//*[@id="div5"]/div[2]/div[%s]' % str(random.randint(1, 2))
    answer_5 = driver.find_elements_by_xpath(xpath5)[0]
    answer_5.click()

    ### 多选题。
    qList_6 = [str(x) for x in range(1, 6)]
    aList_6 = random.sample(qList_6, 3)
    for i in aList_6:
        xpath6 = '//*[@id="div6"]/div[2]/div[%s]' % i
        answer_6 = driver.find_elements_by_xpath(xpath6)[0]
        answer_6.click()

    qList_7 = [str(x) for x in range(1, 8)]
    aList_7 = random.sample(qList_7, 3)
    for i in aList_7:
        xpath7 = '//*[@id="div7"]/div[2]/div[%s]' % i
        answer_7 = driver.find_elements_by_xpath(xpath7)[0]
        answer_7.click()

    qList_8 = [str(x) for x in range(1, 7)]
    aList_8 = random.sample(qList_8, 3)
    for i in aList_8:
        xpath8 = '//*[@id="div8"]/div[2]/div[%s]' % i
        answer_8 = driver.find_elements_by_xpath(xpath8)[0]
        answer_8.click()

    xpath9 = '//*[@id="div9"]/div[2]/div[%s]' % str(random.randint(1, 5))
    answer_9 = driver.find_elements_by_xpath(xpath9)[0]
    answer_9.click()

    xpath10 = '//*[@id="div10"]/div[2]/div[%s]' % str(random.randint(1, 3))
    answer_10 = driver.find_elements_by_xpath(xpath10)[0]
    answer_10.click()

    xpath11 = '//*[@id="div11"]/div[2]/div[%s]' % str(random.randint(1, 5))
    answer_11 = driver.find_elements_by_xpath(xpath11)[0]
    answer_11.click()

    xpath12 = '//*[@id="div12"]/div[2]/div[%s]' % str(random.randint(1, 4))
    answer_12 = driver.find_elements_by_xpath(xpath12)[0]
    answer_12.click()

    # 判断页面是否有验证码
    captcha = ifcaptcha()
    if captcha:
        captchaCode()
        code = identify()
        print('验证码:%s' % code)
        inpu = driver.find_elements_by_css_selector('#yucinput')[0]
        inpu.send_keys(code)


    submit = driver.find_elements_by_css_selector('#ctlNext')[0]
    submit.click()

    try:
        finished = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CSS_SELECTOR, "#tbAward > tbody > tr:nth-child(2) > td:nth-child(2) > a"))
        )
        print('————————第%d页已完成————————' % index)
    except:
        print('————————第%d页失败!————————' % index)

def ifcaptcha():
    # 判断页面是否出现验证码,存在返回True。
    captcha_submit = driver.find_elements_by_css_selector('#tdCode')[0]
    if captcha_submit.get_attribute('style')[9:14] == 'block':
        return True
    else:
        return False

def captchaCode():
    inpu = driver.find_elements_by_css_selector('#yucinput')[0]
    inpu.click()
    time.sleep(0.5)
    img = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "#imgCode"))
    )
    # 截取整个网页
    driver.save_screenshot('wholeWeb.png')
    # 定位验证码位置
    left =img.location['x']
    top = img.location['y']
    right = img.location['x'] + img.size['width']
    bottom = img.location['y'] + img.size['height']

    im = Image.open('wholeWeb.png')
    im = im.crop((left, top, right, bottom))
    im.save('captcha.png')

def identify():
    '''
    验证码识别
    :return: 验证码
    '''
    chaojiying = Chaojiying_Client('超级鹰账号', '超级鹰密码', '软件key')
    im = open('captcha.png', 'rb').read()
    return chaojiying.PostPic(im, 1902)['pic_str']


if __name__ == '__main__':
    driver = webdriver.PhantomJS()
    for index in range(1,51):
        autoSelect()

验证码识别,chaojiying.py

此文件不用动。

import requests
from hashlib import md5

class Chaojiying_Client(object):

    def __init__(self, username, password, soft_id):
        self.username = username
        password =  password.encode('utf8')
        self.password = md5(password).hexdigest()
        self.soft_id = soft_id
        self.base_params = {
            'user': self.username,
            'pass2': self.password,
            'softid': self.soft_id,
        }
        self.headers = {
            'Connection': 'Keep-Alive',
            'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
        }

    def PostPic(self, im, codetype):
        """
        im: 图片字节
        codetype: 题目类型 参考 http://www.chaojiying.com/price.html
        """
        params = {
            'codetype': codetype,
        }
        params.update(self.base_params)
        files = {'userfile': ('ccc.jpg', im)}
        r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
        return r.json()

    def ReportError(self, im_id):
        """
        im_id:报错题目的图片ID
        """
        params = {
            'id': im_id,
        }
        params.update(self.base_params)
        r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
        return r.json()

执行

执行时,这两个文件要放在同一个目录下,而且chaojiying.py文件名不能更改。打开pycharm,在wenjuanxing.py右键,执行。

祝大家代刷成功。

有什么问题,评论区留言,或者留下联系方式。我经常在线。

猜你喜欢

转载自blog.csdn.net/qq_42776455/article/details/82776396