Python抢票实战脚本

首先,导入我们脚本中所需第三方库

import time #休眠设置
from selenium import webdriver # 模拟浏览器
from selenium.webdriver import Keys # 文本清除,重置
from selenium.webdriver.common.by import By # 定位

设置浏览器参数

# 设置浏览器参数,并打开浏览器
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches',['enable-automation'])# 禁用自动化栏
prefs = {"profile.password_manager_enabled": False, "credentials_enable_service": False}
options.add_experimental_option('prefs',prefs) # 关闭密码
options.add_argument('--disable-blink-features=AutomationControlled') # 屏蔽反爬虫特征
options.add_experimental_option("detach", True) # 设置option.add_experimental_option("detach", True)不自动关闭浏览器
options.binary_location = r"D:\装机必备\Google\Chrome\Application\chrome.exe" # Chrome文件地址
driver = webdriver.Chrome(options=options) # 创建
rof= open('stealth.min.js',mode='r',encoding='utf-8').read()# 反扒文件读取
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',{'source':rof}) # 移除爬虫特征

跳转至某抢票网页

driver.get("http://www.xxxxxx.com.")

点击登录

# 点击密码登录driver.find_element(By.XPATH,"/html/body/div[15]/div[2]/div[2]").click()# 输入账号
driver.find_element(By.XPATH,'/html/body/div[15]/form[2]/div[1]/input').send_keys('账号')# 输入密码
driver.find_element(By.XPATH,'/html/body/div[15]/form[2]/div[2]/input').send_keys('密码')# 点击登录
driver.find_element(By.XPATH,'/html/body/div[15]/form[2]/div[5]/input').click()
time.sleep(0.5) # 休眠处理,等待元素加载

选择需要观看场次,人数以及票价

# 点击场次
driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[1]/div[3]/div/div[1]/div[2]/div[1]').click()
time.sleep(0.5)# 休眠处理# 选择档次
driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[1]/div[3]/div/div[3]/div[2]/div[1]').click()
time.sleep(0.5)# 休眠处理# 选择数量为2,若一人观看,则注释该行
driver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div[1]/div[3]/div/div[4]/div[2]/div[2]').click()# 定位直接购票按钮
driver.find_element(By.XPATH,"/html/body/div[2]/div[2]/div[1]/div[3]/div/div[6]/div/div/div[1]").click()

填写电子信息

# 填写信息
driver.find_element(By.XPATH,'//*[@class="input input receiver-name-4"]').send_keys(Keys.CONTROL+'a') # 全选
driver.find_element(By.XPATH,'//*[@class="input input receiver-name-4"]').send_keys(Keys.DELETE)# 清空
driver.find_element(By.XPATH,'//*[@class="input input receiver-name-4"]').send_keys("电子信息")# 重新填写

填写观看人信息

# 实名填写
driver.find_element(By.XPATH,'/html/body/div[4]/div[2]/p/input[1]').send_keys("用户一")
driver.find_element(By.XPATH,'/html/body/div[4]/div[2]/p/input[2]').send_keys("身份信息")
driver.find_element(By.XPATH,'/html/body/div[4]/div[2]/p[2]/input[1]').send_keys("用户二")
driver.find_element(By.XPATH,'/html/body/div[4]/div[2]/p[2]/input[2]').send_keys("身份信息")

确认订单

# 确认订单
driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[3]/div[1]').click()

完成支付即可

本文链接:https://my.lmcjl.com/post/9447.html

展开阅读全文

4 评论

留下您的评论.