以前帮朋友做的抢答脚本,虽然最后没有软用(因为最后抢的时候页面压根打不开),不过在这里分享一下代码以及思路。
首先,说说设计吧,脚本使用的是selenium+python2,因为在抢答之前我连问卷有啥字段都不知道。所以只能建立一个可能的回答,到时候把可能能自动补入的字段填进去。所以脚本是半自动的,当然,如果问卷是固定字段的就可以全自动了。
config_dict = {
'sfz':'33011111111111111', # 太長了
'csrq':u'1993-1-1',
'sjhm':'123456789',
'xm':u'张三',
'qt':'',
'xb':2,
'zz':u'杭州'
}
for n in range(1,len(question_titles)+1,1):
question_title = question_titles[n-1]
print question_title
if '身份证' in question_title:
question[n] = 'sfz'
elif '手机' in question_title or '联系' in question_title or '电话' in question_title:
question[n] = 'sjhm'
elif '姓名' in question_title:
question[n] = 'xm'
elif '性别' in question_title:
question[n] = 'xb'
elif '生日' in question_title or '出生' in question_title:
question[n] = 'csrq'
elif '地址' in question_title or '住址' in question_title:
question[n] = 'zz'
else:
question[n] = 'qt'
这里碰到一个问题,就是日期插件的自动选择 移除上面的readonly属性即可把选择形式的日期控件改成直接填值。
if 'csrq'==qname:
cop.exec_js("var setDate=document.getElementById(\"q%s\");setDate.removeAttribute('readonly');"%n);
还有一个选择性别的单选框插件,这里我默认选择第二个。
elif 'xb' == qname:
button = cop.find_elements_by_xpath("//div[@for='q%s_2']"%n)[0]
Action = TouchActions(cop.driver)
Action.tap(button)
Action.perform()
最后,控制脚本到点才开始刷新页面
while True:
time.sleep(1)
if (datetime.datetime.now().hour>=13 and datetime.datetime.now().minute>=59) or (datetime.datetime.now().hour>=14): # 18点59开抢
break
最终效果:在时间点之前运行代码,然后脚本会一秒刷新一次页面,然后能抢了之后会马上自动填写吻合预先设置的字段,然后点击提交。
完整代码
# -*- coding: utf-8 -*-
"""
File Name: wenjuanxin
Description :
Author : meng_zhihao
date: 2018/9/4
淘宝店地址 :https://shop560916306.taobao.com/?spm=2013.1.1000126.2.3418ab83wv5Ai2
"""
import requests
from lxml import etree
import json
import urllib
import time
import random
import datetime
import csv
import os
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.touch_actions import TouchActions
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import ConfigParser
import re
import datetime
import pdb
class Actions(ActionChains):
def wait(self, time_s):
self._actions.append(lambda: time.sleep(time_s))
return self
class ChromeOperate():
def __init__(self,url='',executable_path='',User_data_dir='',arguments=[],headless=True,mod='pc'):
option = webdriver.ChromeOptions()
if User_data_dir:
option.add_argument( '--user-data-dir=%s'%User_data_dir) # 设置成用户自己的数据目录
else:
import getpass
username = getpass.getuser()
default_path = 'C:\Users\%s\AppData\Local\Google\Chrome\User Data'%username #echo %LOCALAPPDATA%\Google\Chrome\User Data
if os.path.exists(default_path):
#option.add_argument('--user-data-dir=%s' % default_path)
pass
if mod=='wx':
mobile_emulation = {'deviceName': 'iPhone 6'}
option.add_experimental_option("mobileEmulation", mobile_emulation)
option.add_argument('--start-maximized')
if headless:option.add_argument('headless')
option.add_argument('google-base-url=%s' % 'https://www.baidu.com/')
for argument in arguments:
option.add_argument(argument)
if not executable_path:executable_path=r'C:\Users\Administrator\Desktop\chromedriver.exe'
self.driver = webdriver.Chrome(executable_path=executable_path,chrome_options=option)
if url:self.open(url)
def open(self,url):
self.driver.get(url) # self.driver.get(url).page_source
def open_source(self):
return self.driver.page_source
def title(self):
self.title=self.driver.title
return self.title
def quit(self):
self.driver.quit()
def find_element_by_name(self,name):
return self.driver.find_element_by_name(name)
def find_elements_by_xpath(self,xpath): #貌似不能用/text
return self.driver.find_elements_by_xpath(xpath)
def find_element_by_id(self,id):
try:
return self.driver.find_element_by_id(id)
except:
return None
def input_words(self,element,words):
element.clear()
element.send_keys(str(words))
def click_by_id(self,id):
self.driver.find_element_by_id(id).click()
def send_file(self,element,path):
element.sendKeys(path);
def wait_element(self,element_id):
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.ID, element_id))
)
def get_title(self):
print(self.driver.title)
return self.driver.title
def refresh(self):
self.driver.refresh() #
def exec_js(self,js_script):
self.driver.execute_script(js_script)
cop = ChromeOperate(executable_path=r'chromedriver.exe',headless=False,mod='wx')
config_dict = {
'sfz':'33011111111111111', # 太長了
'csrq':u'1993-1-1',
'sjhm':'123456789',
'xm':u'张三',
'qt':'',
'xb':2,
'zz':u'杭州'
}
question = {
}
def getXpath(xpath, content, charset='utf8', xml_type='HTML'): # xpath操作貌似会把中文变成转码&#xxxx; /text()变unicode编码
tree = etree.HTML(content)
out = []
results = tree.xpath(xpath)
for result in results:
if 'ElementStringResult' in str(type(result)) or 'ElementUnicodeResult' in str(type(result)):
out.append(result)
else:
out.append(etree.tostring(result, encoding=charset, method=xml_type)) # 加编码就不会变成html编码了
return out
def get_questions(page_buf):
# 解析 q1-q6 都是什么东西
# pdb.set_trace()
question_titles = getXpath('//div[@class="field-label"]',page_buf)
for n in range(1,len(question_titles)+1,1):
question_title = question_titles[n-1]
print question_title
if '身份证' in question_title:
question[n] = 'sfz'
elif '手机' in question_title or '联系' in question_title or '电话' in question_title:
question[n] = 'sjhm'
elif '姓名' in question_title:
question[n] = 'xm'
elif '性别' in question_title:
question[n] = 'xb'
elif '生日' in question_title or '出生' in question_title:
question[n] = 'csrq'
elif '地址' in question_title or '住址' in question_title:
question[n] = 'zz'
else:
question[n] = 'qt'
if __name__=='__main__':
timestamp=datetime.datetime.now().strftime("%Y%m%d%H%M%S")
print timestamp
#max_num = int(mainconf.get('conf', 'max_num'))
while True:
time.sleep(1)
if (datetime.datetime.now().hour>=13 and datetime.datetime.now().minute>=59) or (datetime.datetime.now().hour>=14): # 18点59开抢
break
while True:
cop.open('https://www.wjx.top/m/34952592.aspx')
try:
source = cop.open_source()
if '到时再重新打开' in source:
time.sleep(1)
continue # 检测是否可以填写
get_questions(source)
qn = len(question.keys())+1
for n in range(1,qn,1):
try:
qname = question[n]
print qname
if 'csrq'==qname:
cop.exec_js("var setDate=document.getElementById(\"q%s\");setDate.removeAttribute('readonly');"%n);
qvalue = config_dict.get(qname,'')
data_ele = cop.find_element_by_id('q%s'%n)
if data_ele:
data_ele.send_keys(qvalue)
elif 'xb' == qname:
button = cop.find_elements_by_xpath("//div[@for='q%s_2']"%n)[0]
Action = TouchActions(cop.driver)
Action.tap(button)
Action.perform()
except Exception as e:
print str(e)
#pdb.set_trace()
# cop.click_by_id('ctlNext') # 为啥
time.sleep(0.5)
button = cop.find_element_by_id('ctlNext')
Action = TouchActions(cop.driver)
Action.tap(button)
Action.perform()
break
except Exception as e:
print str(e)
time.sleep(100)
time.sleep(6000) # 全流程重试间隔
# 据说不频繁是不会出验证码的!
# 填不出来的用不详?
# 选项不够的情况下不要继续点击 ,暂停掉
# 系统时间核对
# 半自动思路 手动复制字段就可以在粘贴板里生成对应信息