问卷星python+selenium自动填写

1.下载插件文件stealth.min.js
链接:https://pan.baidu.com/s/18XV6ROkSg-FEnADyY6AHeA
提取码:bpee
2.自动填写过程中发现挂梯子不用弹出滑动验证,用本地ip刷到几十份就挂了
3.代码

from selenium.webdriver.support.ui import WebDriverWait
import time
import random
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
def get_ele_time(driver,time,func):
    WebDriverWait(driver,time).until(func)

def run():
    # browser = webdriver.Chrome()
    # browser.get("https://www.wjx.cn/vm/YsK8J1l.aspx")
    chrome_options = Options()
    chrome_options.add_argument("--disable-blink-features=AutomationControlled")
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

    # chrome_options.add_argument("--headless")
    chrome_options.add_argument(
        'user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36')

    browser = Chrome(options=chrome_options)

    with open('D:\Afjy\爬虫\模拟请求/stealth.min.js') as f:
        js = f.read()

    browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": js,
    })
    browser.get('https://www.wjx.cn/vm/YsK8J1l.aspx')
    # time.sleep(2)

    # 1
    randomId = random.randint(1, 2)
    js = "document.getElementById(\"q1_" + str(randomId) + "\").click()"
    browser.execute_script(js)
    time.sleep(1)

    # 2
    randomId = random.randint(2, 3)
    js = "document.getElementById(\"q2_" + str(randomId) + "\").click()"
    browser.execute_script(js)
    time.sleep(1)

    # 3
    randomId = random.randint(1, 4)
    js = "document.getElementById(\"q3_" + str(randomId) + "\").click()"
    browser.execute_script(js)
    time.sleep(1)

    # 4
    randomId = random.randint(1, 3)
    js = "document.getElementById(\"q4_" + str(randomId) + "\").click()"
    browser.execute_script(js)
    time.sleep(1)

    # 5
    randomId1 = random.randint(1, 6)
    js = "document.getElementById(\"q5_" + str(randomId1) + "\").checked = true"
    browser.execute_script(js)
    js = "document.getElementById(\"q5_" + str(randomId1) + "\").click()"
    browser.execute_script(js)

    # 6
    randomId = random.randint(1, 2)
    js = "document.getElementById(\"q6_" + str(randomId) + "\").click()"
    browser.execute_script(js)
    time.sleep(1)

    # 8
    randomId = random.randint(1, 4)
    js = "document.getElementById(\"q8_" + str(randomId) + "\").click()"
    browser.execute_script(js)
    time.sleep(1)

    # 9
    randomId = random.randint(1, 3)
    js = "document.getElementById(\"q9_" + str(randomId) + "\").click()"
    browser.execute_script(js)
    time.sleep(1)

    # 10
    # randomId = random.randint(1, )
    js = "document.getElementById(\"q10_" + str(1) + "\").click()"
    browser.execute_script(js)
    block = ['1', "2", "1.5"]
    randomId = random.randint(0, 2)
    browser.find_element_by_id("tqq10_1").send_keys(block[randomId])
    # time.sleep(1)
    time.sleep(1)

    # 11
    randomId1 = random.randint(1, 4)
    js = "document.getElementById(\"q11_" + str(randomId1) + "\").checked = true"
    browser.execute_script(js)
    js = "document.getElementById(\"q11_" + str(randomId1) + "\").click()"
    browser.execute_script(js)

    # 12
    randomId1 = random.randint(1, 4)
    js = "document.getElementById(\"q12_" + str(randomId1) + "\").checked = true"
    browser.execute_script(js)
    js = "document.getElementById(\"q12_" + str(randomId1) + "\").click()"
    browser.execute_script(js)

    # 13
    randomId1 = random.randint(1, 5)
    js = "document.getElementById(\"q13_" + str(randomId1) + "\").checked = true"
    browser.execute_script(js)
    js = "document.getElementById(\"q13_" + str(randomId1) + "\").click()"
    browser.execute_script(js)

    # 14
    randomId1 = random.randint(1, 4)
    js = "document.getElementById(\"q14_" + str(randomId1) + "\").checked = true"
    browser.execute_script(js)
    js = "document.getElementById(\"q14_" + str(randomId1) + "\").click()"
    browser.execute_script(js)

    #15
    js = "document.getElementById(\"q15_" + str(1) + "\").click()"
    browser.execute_script(js)
    time.sleep(1)

    #16
    randomId1 = random.randint(1, 2)
    js = "document.getElementById(\"q16_" + str(randomId1) + "\").checked = true"
    browser.execute_script(js)
    js = "document.getElementById(\"q16_" + str(randomId1) + "\").click()"
    browser.execute_script(js)

    #17
    js = "document.getElementById(\"q17_" + str(1) + "\").click()"
    browser.execute_script(js)
    time.sleep(1)

    #18
    randomId1 = random.randint(1, 6)
    js = "document.getElementById(\"q18_" + str(randomId1) + "\").checked = true"
    browser.execute_script(js)
    js = "document.getElementById(\"q18_" + str(randomId1) + "\").click()"
    browser.execute_script(js)

    #19
    js = "document.getElementById(\"q19_" + str(1) + "\").click()"
    browser.execute_script(js)
    time.sleep(1)

    #20
    randomId1 = random.randint(1, 5)
    js = "document.getElementById(\"q20_" + str(randomId1) + "\").checked = true"
    browser.execute_script(js)
    js = "document.getElementById(\"q20_" + str(randomId1) + "\").click()"
    browser.execute_script(js)

    # 21
    block = ["问卷内容还有点不够细致", "问卷内容很全面,符合实际", "没有不足之处,挺好的"]
    randomId = random.randint(0, 2)
    browser.find_element_by_id("q21").send_keys(block[randomId])
    # time.sleep(1)


    # submit
    js = "document.getElementById(\"ctlNext\").click()"
    browser.execute_script(js)

    # 消除确认弹窗
    time.sleep(1)
    js = 'document.getElementById("alert_box").style.display="none";'
    browser.execute_script(js)

    # 点智能验证提示框,进行智能验证
    browser.find_element_by_xpath("//div[@id='rectMask']").click()


    #
    #
    time.sleep(7)
    browser.quit()  # 运行完毕自动关闭浏览器


if __name__ == "__main__":
    for i in range(1,70):
        print(i)
        run()
        time.sleep(2)	#避免提交过快出现验证)

你可能感兴趣的:(python爬虫实践,python,selenium,chrome)