takeaway
Questionnaire star, I think we more or less in the university or work may encounter some, some work often have to fill in, this can be quite our senior often complain about, I say it’s anonymous questionnaire, the results of general to fill up quite a long time to finish, if more than is really boring, and the university of our side will often encountered, such as joining a club, need to fill in all kinds of information, Then various options, school leaders sometimes send some of this questionnaire to let us fill in anonymous, a questionnaire is a way of discovering the fact status of research, the biggest purpose is to collect, accumulated a certain target groups of the basic information of science education attribute, can be divided into two types: descriptive research and analytical research. When deciding whether to use questionnaire as a research tool, we should consider whether the research objectives can be successfully achieved and pay attention to the degree of cooperation of research samples in the questionnaire. For contemporary college students, ma Yuansi’s practice activities in these courses are inseparable from the questionnaire survey, but after all kinds of forwarding, only a few people fill in the pain who understand?
Nonsense not to say let’s demonstrate, after all, there will be a demand for the market! Will use their own things to realize this is to rely on technology to eat, every month more pocket money it is not sweet?
start
Selenium in Python is a tool that simulates clicking to fill out a questionnaire. However, most companies and schools now have anti-crawl technology. When you simulate clicking the submit button, an intelligent verification will pop up, and the background determines that you are using Python to control the browser. Whether you manually click or automatically click will prompt the verification failure.
The basic principle of detection is to check whether the window.navigator object in the current browser window contains the webDriver property. Normally, this value is undefined. When we use Selenium, this property will be assigned a value. It just passes our operation.
Pyppeteer, a third-party library, can be used to solve part of the anti-crawling technology. This article does not use this method. We can execute JS code before the page entry, and empty the WebDriver attribute, so as to avoid the site detection
option = webdriver.ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=option)
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',{'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'})
driver.get("https://www.wjx.cn/m/47846766.aspx")
Copy the code
The following are the random multiple-choice and fill-in-the-blank questions. Selenium’s eight positioning methods are very simple. You can choose the answers at random based on your preferences, and the fill-in-the-blank questions can be obtained from a preset list
For I in range(1,11): n=random.randint(1,3) n1=random.randint(1,2) time.sleep(0.3) try: element=driver.find_element_by_css_selector("#div{} > div.ui-controlgroup > div:nth-child({})".format(i,n)) element.click() except: try: element = driver.find_element_by_css_selector("#div{} > div.ui-controlgroup > div:nth-child({})".format(i, n1)) element.click() except: pass input = driver.find_element_by_id("q11") input.send_keys(word)Copy the code
If the submission is too frequent, the verification will still be triggered, but at this time, the system has treated us as a real person, and we only need to click the button of intelligent verification once to pass the verification and complete the questionnaire
time.sleep(1) try: element = driver.find_element_by_css_selector("#alert_box > div:nth-child(2) > div:nth-child(2) > button") Element.click () time.sleep(0.5) yanz = driver.find_element_by_id("rectMask") yanz.click() time.sleep(4) except: pass driver.close()Copy the code
You can see that the randomness of the questionnaire is quite good
Now it’s time to code!
Import time from Selenium import webdriver import random j=1 word=' fill in the blank 'while(j<51): # chrome_options = webdriver.ChromeOptions() # chrome_options.add_argument('--headless') # driver = webdriver.Chrome(options=chrome_options) # driver = webdriver.Chrome() option = webdriver.ChromeOptions() option.add_experimental_option('excludeSwitches', ['enable-automation']) option.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=option) driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',{'source': 'Object.defineProperty(navigator, "webdriver", {get: () = > 'undefined})}) driver. The get (" https://www.wjx.cn/m/47846766.aspx ") time. Sleep (2) for I in range (1, 11) : N = random. Randint n1 = (1, 3). The random randint (1, 2) time. Sleep (0.3) try: element=driver.find_element_by_css_selector("#div{} > div.ui-controlgroup > div:nth-child({})".format(i,n)) element.click() except: try: element = driver.find_element_by_css_selector("#div{} > div.ui-controlgroup > div:nth-child({})".format(i, n1)) element.click() except: Pass input = driver.find_element_by_id("q11") input.send_keys(word) time.sleep(0.5) tijiao=driver.find_element_by_id("ctlNext") tijiao.click() time.sleep(1) try: element = driver.find_element_by_css_selector("#alert_box > div:nth-child(2) > div:nth-child(2) > button") Element.click () time.sleep(0.5) yanz = driver.find_element_by_id("rectMask") yanz.click() time.sleep(4) except: Pass driver.close() print(" submitted {} times ". Format (j)) j=j+1Copy the code
Code is not a lot, quite simple! Fifty times like this, even if two pieces a time is a hundred ah, so that there is a demand will certainly have the market!
More than 3000 Python ebooks are available, including Python400, Python400, Python400, Python400 and Python400
Complete source code
You can just take it if you need it and click collect.