大麦网抢票脚本【Python脚本】
立即下载
资源介绍:
Python脚本,使用Selenium 模拟浏览器操作。
在使用 Chrome 浏览器,用户可以使用鼠标滑动、按键点击以及键盘输入,作为信号输入设备向浏览器传达指令,浏览器收到指令后执行渲染。
这里提到的 Selenium WebDriver 是对浏览器提供的原生 API 进行封装,使用这套 API 可以操控浏览器的开启、关闭,打开网页,操作界面元素,控制 Cookie。简单说就是,可以通过写代码的方式来自动实现用户鼠标和键盘信号的输入。
由此实现模拟人为操作进行登录、验证、刷新网页以及点击购票等操作。
"""
大麦网自动抢票脚本
Python学习交流q群:708525271
VIP课程咨询添加微信:Python1018
"""
import time
from selenium.webdriver.support.wait import WebDriverWait
from info import PHONE, PASSWORD, TARGET_URL
from selenium import webdriver
from selenium.webdriver.common.by import By
def get_tracks(distance):
v = 5
t = 0.2
tracks = []
current = 0
mid = distance * 4 / 5
while current < distance:
if current < mid:
a = 8
else:
a = -3
v0 = v
s = v0 * t + 0.5 * a * (t ** 2)
current += s
tracks.append(round(s))
v = v0 + a * t
return tracks
login_url = "https://passport.damai.cn/login?ru=https%3A%2F%2Fwww.damai.cn%2F"
# target_url = "https://detail.damai.cn/item.htm?spm=a2oeg.home.card_0.ditem_3.591b23e1jhP4Ro&id=694591586226"
target_url = "https://detail.damai.cn/item.htm?spm=a2oeg.search_category.0.0.50b51e33rO1nzu&id=727698795527"
# target_url = "https://detail.damai.cn/item.htm?spm=a2oeg.search_category.0.0.6f774d15bogB0c&id=727150053451"
# 浏览器配置对象
options = webdriver.ChromeOptions()
# 禁用自动化栏
options.add_experimental_option('excludeSwitches', ['enable-automation'])
# 屏蔽保存密码提示框
prefs = {'credentials_enable_service': False, 'profile.password_manager_enabled': False}
options.add_experimental_option('prefs', prefs)
# 反爬虫特征处理
options.add_argument('--disable-blink-features=AutomationControlled')
# 打开浏览器
driver = webdriver.Chrome(executable_path='chromedriver.exe', options=options)
f = open('stealth.min.js', mode='r', encoding='utf-8').read()
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': f})
driver.get(login_url)
try:
driver.switch_to.frame(0)
# driver.find_element_by_css_selector('#fm-login-id').send_keys(PHONE)
driver.find_element(by=By.CSS_SELECTOR, value='#fm-login-id').send_keys(PHONE)
# driver.find_element_by_css_selector('#fm-login-password').send_keys(PASSWORD)
driver.find_element(by=By.CSS_SELECTOR, value='#fm-login-password').send_keys(PASSWORD)
time.sleep(1)
driver.switch_to.frame(0)
# slider = driver.find_element_by_css_selector('#nc_1_n1z')
slider = driver.find_element(by=By.CSS_SELECTOR, value='#nc_1_n1z')
webdriver.ActionChains(driver).click_and_hold(on_element=slider).perform()
webdriver.ActionChains(driver).move_by_offset(xoffset=260, yoffset=0).perform()
webdriver.ActionChains(driver).pause(0.5).release().perform()
driver.switch_to.parent_frame()
except:
print("没有遇到滑块")
# driver.find_element_by_css_selector('#login-form > div.fm-btn > button').click()
driver.find_element(by=By.CSS_SELECTOR, value='#login-form > div.fm-btn > button').click()
time.sleep(2)
driver.get(TARGET_URL)
time.sleep(1)
# driver.find_element_by_css_selector('.buybtn').click()
# driver.find_element(by=By.CSS_SELECTOR, value='.buybtn').click()
while 1:
try:
a = 1
a = driver.find_element(by=By.CSS_SELECTOR, value='.unsale > .words').text
if a == '您可以尝试去手机购买':
driver.refresh()
time.sleep(0.5)
except:
break
driver.find_element(by=By.CSS_SELECTOR, value='.buy-link').click()
# 设置等待
wait = WebDriverWait(driver, 10, 0.5)
# 使用匿名函数
wait.until(lambda diver: driver.find_element(by=By.XPATH, value='//*[@class="iconfont icondanxuan-weixuan_"]'))
# driver.find_element(by=By.CSS_SELECTOR, value='tpl-wrapper').click()
time.sleep(1)
driver.find_element(by=By.XPATH, value='//*[@class="iconfont icondanxuan-weixuan_"]').click()
# path = driver.find_element(by=By.XPATH, value='//*[@class="iconfont icondanxuan-weixuan_"]') driver.execute_script(
# "arguments[0].setAttribute(arguments[1],arguments[2])", path, 'class', 'iconfont icondanxuan-xuanzhong_')
time.sleep(0.5)
driver.find_element(by=By.XPATH, value='//span[contains(text(),"提交订单")]').click()
time.sleep(50)