侧边栏壁纸
博主头像
Excalibur博主等级

真实自有万钧之力。

  • 累计撰写 25 篇文章
  • 累计创建 14 个标签
  • 累计收到 2 条评论

利用Selenium秒填朋友圈各种问卷星调查问卷

Excalibur
2020-05-13 / 0 评论 / 0 点赞 / 357 阅读 / 2,792 字
温馨提示:
本文最后更新于 2022-04-07,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

0️⃣ 前言

毕业季到了,要开始写论文了,朋友圈各种同学的各种课题的调查问卷,但几乎没什么人填,想帮他们随机填一填。

1️⃣ 实现功能

  • 目前还不算完善,只能填电脑端,手机端需要重新写逻辑,也就是链接里面要改成jq而不是m
  • 默认自动填10份,可以在主函数的times里面修改数值;
  • 问卷链接在FillTheQuestionaire函数里面修改;
  • 写了代理更换,但没钱换代理,换了代理可以更加真实一点,不然老是一个地区的人填不好。

2️⃣ 代码

from selenium import webdriver
import time
import re
import os
from bs4 import BeautifulSoup
from lxml import etree
import random
import pandas as pd

def ChangeIP():
    page = random.randint(1,4055)
    url = 'https://www.xicidaili.com/nn/'
    # url = 'https://www.kuaidaili.com/free/'
    driverIP = webdriver.Chrome()
    driverIP.get(url)
    content = driverIP.page_source.encode('utf-8')
    html = etree.HTML(content)
    t = html.xpath("//div[@class='bar']/div[@class='bar_inner fast']")
    flow = ['99%','98%']
    for i in t:
        if i.attrib['style'].split(":")[1] in flow:
            index = t.index(i)
            break
    driverIP.quit()
    data = pd.read_html(content)
    ipinfo = data[0].values[index]
    # ipinfo = random.choice(data[0].values)
    ip = str(ipinfo[5]).lower() + "://" + str(ipinfo[1]) + ":" + str(ipinfo[2])
    # ip = str(ipinfo[3]).lower() + "://" + str(ipinfo[0]) + ":" + str(ipinfo[1])
    return ip

def FillTheQuestionaire(times):
    url = 'https://www.wjx.cn/jq/74385885.aspx'
    for t in range(times):
        mobileEmulation = {'deviceName': 'iPhone X'}
        options = webdriver.ChromeOptions()
        options.add_experimental_option('mobileEmulation', mobileEmulation)
        # options = webdriver.ChromeOptions()
        # ip = ChangeIP()
        # print(ip)
        # options.add_argument("--proxy-server=" + ip)
        # driver = webdriver.Chrome(chrome_options=options)
        # if t % 2 == 0:
        driver = webdriver.Chrome()
        # else:
        # driver = webdriver.Chrome(chrome_options=options)
        driver.get(url)
        content = driver.page_source.encode('utf-8')
        html = etree.HTML(content)
        soup = BeautifulSoup(content, 'lxml')
        NumOfQuestions = len(driver.find_elements_by_xpath(
            "//div[@class='div_question']"))
        for quiz in range(NumOfQuestions):
            try:
                question = driver.find_elements_by_xpath("//div[@id='divquestion" + str(
                    quiz + 1) + "']//ul[@class='ulradiocheck']//li//a[@class='jqRadio']")
                random.choice(question).click()
            except:
                pass
            try:
                tr = driver.find_elements_by_xpath(
                    "//div[@id='divquestion" + str(quiz + 1) + "']/table/tbody/tr")
                for t in range(len(tr)):
                    button = driver.find_elements_by_xpath("//div[@id='divquestion" + str(
                        quiz + 1) + "']/table/tbody/tr[" + str(t + 1) + "]/td/a[@class='jqRadio']")
                    try:
                        random.choice(button).click()
                    except:
                        pass
            except:
                pass

            try:
                checkbox = driver.find_elements_by_xpath("//div[@id='divquestion" + str(
                    quiz + 1) + "']//ul[@class='ulradiocheck']//li//a[@class='jqCheckbox']")
                YorN = [x for x in range(2)]
                checkbox[0].click()
                for i in range(len(checkbox) - 2):
                    if random.choice(YorN) == 1:
                        print("是")
                        try:
                            checkbox[i+1].click()
                        except:
                            pass
            except:
                pass
        time.sleep(3)
        driver.find_elements_by_xpath("//input[@id='submit_button']")[0].click()
        time.sleep(2)
        driver.quit()


if __name__ == "__main__":
    times = 10
    FillTheQuestionaire(times)

0

评论区