1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
| """ 基于python和selenium实现的某麦网自动刷新抢票脚本 用户要提前添加好个人信息和收货地址 """
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException import time
URL = 'http://m.damai.cn/damai/perform/item.html?projectId=146290'
MIN = 0 USERNAME = "13112390306"
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 5) driver.get(URL)
""" PC端网页抢票操作 """ def choose(seletor): try: choice = wait.until(EC.element_to_be_clickable((By.XPATH, seletor))) return choice except TimeoutException as e: print("Time out!") return None except Exception: print("Not found!") return None
def login(): login = choose('//*[@id="userLoginInfo"]/span/a[1]') login.click() username = choose('//*[@id="login_email"]') username.send_keys(USERNAME) """ 由于密码框控件被设置为不可见 先自行输入密码并记住密码 方便刷新 (也可用cookie实现) """ password = choose('//*[@id="login_pwd_txt"]') try: password.click() password.send_keys("********") except Exception: print(password) print("Password Can't click")
def buy(): try: price = None plus = None buybtn = None submit = None booker = None select = None confirm = None driver.get(URL) while None == price: price = choose('//*[@id="priceList"]/div/ul/li[3]') price.click() while None == plus: plus = choose('//*[@id="cartList"]/div[1]/ul/li/span[3]/a[2]') plus.click() while None == buybtn: buybtn = choose('//*[@id="btnBuyNow"]') driver.execute_script("arguments[0].scrollIntoView();", buybtn) buybtn.click() while None == booker: booker = choose('/html/body/div[3]/div[3]/div[2]/div[2]/div/a') driver.execute_script("arguments[0].scrollIntoView();", booker) booker.click() while None == select: select = choose('/html/body/div[3]/div[3]/div[12]/div/div[2]/div/div[2]/div/table/tbody/tr/label/td[1]/input') driver.execute_script("arguments[0].scrollIntoView();", select) select.click() while None == confirm: confirm = choose('/html/body/div[3]/div[3]/div[12]/div/div[2]/div/p/div/a') driver.execute_script("arguments[0].scrollIntoView();", confirm) confirm.click() while None == submit: submit = choose('//*[@id="orderConfirmSubmit"]') driver.execute_script("arguments[0].scrollIntoView();", submit) submit.click() except Exception: print("抢票失败,尝试重新抢票") buy()
def test(): login() time.sleep(30) print("开始抢票") buy() print("抢票成功")
""" 移动端抢票操作 """
def login_mobile(): """ 点击购买进入登录界面 自行输入帐号密码 """ buybtn = None while None == buybtn: buybtn = choose('/html/body/div[1]/div[2]/div/div[1]/div[2]/div') driver.execute_script("arguments[0].scrollIntoView();", buybtn) buybtn.click() buy = None while None == buy: buy = choose('/html/body/div[1]/div[3]/div[2]/div[1]/div') driver.execute_script("arguments[0].scrollIntoView();", buy) buy.click()
def buy_mobile(): try: buybtn = None while None == buybtn: buybtn = choose('/html/body/div[1]/div[2]/div/div[1]/div[2]/div') driver.execute_script("arguments[0].scrollIntoView();", buybtn) buybtn.click() buy = None while None == buy: buy = choose('/html/body/div[1]/div[3]/div[2]/div[1]/div') driver.execute_script("arguments[0].scrollIntoView();", buy) buy.click() price = None while None == price: price = choose('//html/body/div[1]/div/div[2]/ul/li[3]') driver.execute_script("arguments[0].scrollIntoView();", price) price.click() count = None while None == count: count = choose('/html/body/div[1]/div/div[3]/ul/li/div/div[3]') driver.execute_script("arguments[0].scrollIntoView();", count) count.click() select = None while None == select: select = choose('/html/body/div[1]/div/div[4]/div[3]') driver.execute_script("arguments[0].scrollIntoView();", select) select.click() booker = None while None == booker: booker = choose('/html/body/div[1]/div[1]/div[2]/div[1]/div[2]/div/ul/li/div') driver.execute_script("arguments[0].scrollIntoView();", booker) booker.click() pay = None while None == pay: pay = choose('/html/body/div[1]/div[2]/div[2]/div[2]/div') driver.execute_script("arguments[0].scrollIntoView();", pay) pay.click() except Exception: print("抢票失败,尝试重新抢票") buy_mobile()
def test_mobile(): login_mobile() time.sleep(30) print("开始抢票") buy_mobile() print("抢票成功")
def main(): login() time.sleep(30) while 1: if MIN == time.localtime().tm_min: print("开始抢票") buy() print("抢票成功")
if __name__ == '__main__': test_mobile()
|