1. 程式人生 > >Selenium代刷問卷星問卷,升級版(新手食用)

Selenium代刷問卷星問卷,升級版(新手食用)

前言。

第一個版本做出來的時候只是我自己用的demo,沒想到會有這麼多的同學都需要。所以就做了這個更方便,而且比上個版本來說最大的特點是,不同的電腦都可以執行。(上個版本因為要手動定位驗證碼的位置,不同電腦不一樣,這才做的不用手動除錯。)
因為這是比上一個版本更加優化,就沒有了刷的過程,只有刷的結果。刷的過程效果,請參見:https://blog.csdn.net/qq_42776455/article/details/82151732

先刷個50份試試:
在這裡插入圖片描述在這裡插入圖片描述

環境

  • python3.6.5,python3就可以只要對應的庫安裝成功就好,百度搜索python3安裝教程。
  • pycharm編輯器(有一定經驗的可以不用就相當於執行個指令碼)下載軟體之後對應需要的庫安裝比較方便。需要的庫只需要點選 + ,在彈出的框裡搜尋對應的庫,安裝就OK啦。
    在這裡插入圖片描述
  • 需要安裝的庫,selenium,PIL,requests
  • 超級鷹:http://www.chaojiying.com/ 驗證碼識別API。這個平臺還是比較好的,便宜雖然有時候極個別的會出錯。可以忽略。
    在這裡插入圖片描述
    也就是說你一元錢,可以刷100次,我感覺挺賺的。
    先在該平臺註冊一個賬號(賬號密碼記著,等下要用到。),充值個2元錢。
    打開個人中心。在這裡插入圖片描述

程式碼:

注意第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()

此檔案不用動。

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右鍵,執行。

祝大家代刷成功。

有什麼問題,評論區留言,或者留下聯絡方式。我經常線上。