• 前一段时间有个课程需要问卷星搜集材料信息,,但是问卷星这东西你不一个个求人哪有人愿意点进去帮你填呢,,呵呵,不行,我自己来。。。
  • 本来想看看问卷星他的表单提交的请求方式,,奈何我太垃圾。看不懂那么庞大的js,理不清其中的思路,,既然能力不行,那只好用selenium这个自动化工具了。
  • 很多人学习python,不知道从何学起。
    很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手。
    很多已经做案例的人,却不知道如何去学习更加高深的知识。
    那么针对这三类人,我给大家提供一个好的学习平台,免费领取视频教程,电子书籍,以及课程的源代码!??¤
    QQ群:623406465

先用简单的一个表单做说明吧。找一个简单的问卷

  • 观察dom树结构发现这个rel属性可以确定唯一。那么可以直接用css选择器语法 a[rel=‘q1_2’]python字符串可以用单引号表示,外面套双引号时候不用考虑转义字符串,比较方便。
  • 既然找个这个位置,就可以用程序先模拟点击了,查看中不中。贴上程序代码:
from selenium import webdriver
import time

browser = webdriver.Chrome()#声明
browser.get('https://www.wjx.cn/jq/31521246.aspx')
time.sleep(2)
for i in range(10):
 browser.get('https://www.wjx.cn/jq/31521246.aspx')
 browser.find_element_by_css_selector("a[rel='q1_3']").click()
 time.sleep(1)
 browser.find_element_by_css_selector("a[rel='q2_1']").click()
 time.sleep(1)
 browser.find_element_by_css_selector("a[rel='q3_2']").click()
 time.sleep(1)
 browser.find_element_by_id("submit_button").click()

 

有需要注意的是一定要注意时间间隔,稍微一块就验证码伺候。

  • 之所以用chromediver是因为有界面的可以知道对错,当程序无误的时候可以换成phantomjs。另外,你可以根据已经有的回答情况,和实际情况,按照一定概率进行点击不同的选项,那么就可以更逼真的实现数据的(伪造)??
  • 这篇纯粹娱乐,如大佬能直接破解js请赐教。
  • 如果没有selenium或者没有无头浏览器/chromedriver环境可以百度进行先安装。
    问卷网:

    分析结构之后发现和问卷星的不一样,并且还有多选框,多选框不能为空,我该如何处理呢?
    首先,我将我的问卷给我的好哥们姐们认真的填了一下,采集了14份左右的真实样本。

  • 这样我就可以根据这个数据模拟出很逼真的概率。
  • 说一下这里不一样的地方,这里多个元素用selenium中的find_elements查找多个元素。因为这里不好找到唯一标识,只好通过位置确定元素。
    下面附上python代码,核心思想就是如果差不多就随机编号,如果差得多jiuyong随机100个数查找百分比的范围
 from selenium import webdriver
import time
import random
browser = webdriver.PhantomJS()

browser.get('https://www.baidu.com')
time.sleep(2)

for i in range(200):
  try:
    time.sleep(5)
    browser.get('https://www.wenjuan.com/s/2Uf6Fbe/')
    sex=browser.find_elements_by_css_selector("a[rel='question_5bfe34cd92beb5077b5fdaec']")
    index=random.randint(1,5)
    if index<3:
        sex[1].click()
    else:
        sex[0].click()
    time.sleep(0.5)
    grade=browser.find_elements_by_css_selector("a[rel='question_5bfe34cd92beb5077b5fdaed']")
    #index=random.randint(0,3)
    grade[2].click()
    time.sleep(0.5)
    istanguo=browser.find_elements_by_css_selector("a[rel='question_5bfe34ce92beb5077b5fdaee']")
    index=random.randint(1,10)
    if index>6:
        istanguo[0].click()
    else:
        istanguo[1].click()
    tiaojian=browser.find_elements_by_css_selector("a[rel='question_5bfe34ce92beb5077b5fdaf1']")
    index=random.randint(1,13)
    if index<=6:
        tiaojian[0].click()
    index = random.randint(1, 13)
    if index<=6:
        tiaojian[1].click()
    index = random.randint(1, 13)
    if index <= 3:
        tiaojian[2].click()
    if index<=8:
        tiaojian[3].click()
    index = random.randint(1, 13)
    if index<=3:
        tiaojian[4].click()
    index = random.randint(1, 13)
    if index <= 7:
        tiaojian[5].click()
    if index<=6:
        tiaojian[6].click()
    index = random.randint(1, 13)
    if index<=9:
        tiaojian[7].click()
    index = random.randint(1, 13)
    if index <= 9:
        tiaojian[8].click()
    if index<=2:
        tiaojian[9].click()
    index = random.randint(1, 13)
    if index<=2:
        tiaojian[10].click()
    index = random.randint(1, 16)
    if index <= 2:
        tiaojian[11].click()
    index = random.randint(1, 13)
    if index <= 8:
        tiaojian[12].click()
    age= browser.find_elements_by_css_selector("a[rel='question_5bfe34ce92beb5077b5fdaf4']")
    index=random.randint(0,100)
    if index<50:
        age[0].click()
    elif index>65:
        age[2].click()
    else:
        age[1].click()
    time.sleep(0.5)
    yingxiang=browser.find_elements_by_css_selector("a[rel='question_5bfe34ce92beb5077b5fdafb']")
    index=random.randint(1,10)
    jud=False
    if(index<=4):
        yingxiang[0].click()
        jud=True
    index = random.randint(1, 10)
    if (index <= 2):
        yingxiang[1].click()
        jud = True
    index = random.randint(1, 10)
    if (index <= 6):
        jud = True
        yingxiang[2].click()
    index = random.randint(1, 10)
    if (index <= 8):
        yingxiang[3].click()
        jud = True
    index = random.randint(1, 10)
    if (index <= 5):
        yingxiang[4].click()
        jud = True
    if jud==False:#防止运气不好一个没选上
        yingxiang[2].click()
    time.sleep(0.3)
    jichu=browser.find_elements_by_css_selector("a[rel='question_5bfe34ce92beb5077b5fdafd']")
    index=random.randint(0,100)
    if index<64:
        jichu[1].click()
    elif index>98:
        jichu[2].click()
    else:
        jichu[0].click()

    time.sleep(0.5)
    jiehun=browser.find_elements_by_css_selector("a[rel='question_5bfe395992beb5238625321e']")
    index=random.randint(0,100)
    if index<65:
        jiehun[2].click()
    elif index>98:
        jiehun[0].click()
    elif index>=65 and index<=83:
        jiehun[3].click()
    else:
        jiehun[1].click()
    browser.find_element_by_id("next_button").click()
  except Exception as e:
      print(e)

#input.clear()
#button = browser.find_element_by_class_name('btn-search')
#button.click()

 

查看无误之后不想弹出窗口将chromedriver换成phantomjs或设置不跳出参数即可即可。


可以看到答题人数上涨了(切记控制速度)

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/sn8888/p/13857846.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!