自動(dòng)化搶票的腳本,需通過Selenium庫來實(shí)現(xiàn)自動(dòng)化操作。(學(xué)習(xí)參考)
# 導(dǎo)入必要的庫
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
# 設(shè)置瀏覽器參數(shù)
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
options.add_argument("--start-maximized")
# 創(chuàng)建瀏覽器對(duì)象
browser = webdriver.Chrome(options=options)
# 訪問大麥網(wǎng)登錄頁面
browser.get('https://passport.damai.cn/login?ru=https%3A%2F%2Fwww.damai.cn%2F')
# 等待登錄頁面加載完成
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@class="login-title"]')))
# 輸入賬號(hào)密碼并點(diǎn)擊登錄
username = 'your_username' # 替換為自己的賬號(hào)
password = 'your_password' # 替換為自己的密碼
browser.find_element_by_xpath('//input[@name="username"]').send_keys(username)
browser.find_element_by_xpath('//input[@name="password"]').send_keys(password)
browser.find_element_by_xpath('//button[text()="登錄"]').click()
# 等待登錄成功并跳轉(zhuǎn)到搶票頁面
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@class="page__title"]')))
browser.get('https://detail.damai.cn/item.htm?id=123456') # 替換為自己要搶票的演出鏈接
# 等待搶票頁面加載完成
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@class="buybtn"]')))
# 開始不斷刷新頁面,直到出現(xiàn)“立即購買”按鈕
while True:
try:
browser.refresh()
buy_btn = browser.find_element_by_xpath('//div[@class="buybtn"]')
if buy_btn.text == '立即購買':
break
except:
pass
time.sleep(1)
# 點(diǎn)擊“立即購買”按鈕
buy_btn.click()
# 等待選擇票框加載完成
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@class="select_right"]')))
# 選擇票框、票價(jià)和數(shù)量(這里只選擇一張最便宜的票)
browser.find_element_by_xpath('//span[text()="選擇票框"]').click()
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//ul[@class="perform__select__list"]')))
select_list = browser.find_elements_by_xpath('//ul[@class="perform__select__list"]//li')
select_list[0].click()
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//span[@class="select_right_list_content_price_text"]')))
select_price = browser.find_elements_by_xpath('//span[@class="select_right_list_content_price_text"]')[0]
select_price.click()
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@class
下面是注釋
# 初始化瀏覽器
browser = webdriver.Chrome()
# 找到包含選項(xiàng)的元素列表
select_list = browser.find_elements_by_xpath('//ul[@class="perform__select__list"]//li')
# 點(diǎn)擊第一個(gè)選項(xiàng)
select_list[0].click()
# 等待價(jià)格元素出現(xiàn)
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//span[@class="select_right_list_content_price_text"]')))
# 找到價(jià)格元素
select_price = browser.find_elements_by_xpath('//span[@class="select_right_list_content_price_text"]')[0]
# 點(diǎn)擊價(jià)格元素
select_price.click()
# 等待某個(gè)元素出現(xiàn)
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@class="some_class"]')))
優(yōu)化后的完整代碼:文章來源:http://www.zghlxwxcb.cn/news/detail-509257.html
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
def login_and_buy_ticket(username, password, performance_link):
# 設(shè)置瀏覽器參數(shù)
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
options.add_argument("--start-maximized")
# 創(chuàng)建瀏覽器對(duì)象
with webdriver.Chrome(options=options) as browser:
# 訪問大麥網(wǎng)登錄頁面
browser.get('https://passport.damai.cn/login?ru=https%3A%2F%2Fwww.damai.cn%2F')
# 等待登錄頁面加載完成并輸入賬號(hào)密碼
try:
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@class="login-title"]')))
browser.find_element_by_xpath('//input[@name="username"]').send_keys(username)
browser.find_element_by_xpath('//input[@name="password"]').send_keys(password)
browser.find_element_by_xpath('//button[text()="登錄"]').click()
except Exception as e:
print(f"Error during login: {e}")
return
# 等待登錄成功并跳轉(zhuǎn)到搶票頁面
try:
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@class="page__title"]')))
browser.get(performance_link)
except Exception as e:
print(f"Error navigating to performance page: {e}")
return
# 等待搶票頁面加載完成并開始不斷刷新頁面,直到出現(xiàn)“立即購買”按鈕
while True:
try:
browser.refresh()
buy_btn = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@class="buybtn"]')))
if buy_btn.text == '立即購買':
buy_btn.click()
break
except Exception as e:
print(f"Error while waiting for buy button: {e}")
time.sleep(2)
# 選擇票框、票價(jià)和數(shù)量
try:
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@class="select_right"]')))
browser.find_element_by_xpath('//span[text()="選擇票框"]').click()
select_list = WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.XPATH, '//ul[@class="perform__select__list"]//li')))
select_list[0].click()
select_price = WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.XPATH, '//span[@class="select_right_list_content_price_text"]')))[0]
select_price.click()
except Exception as e:
print(f"Error while selecting tickets: {e}")
# 調(diào)用函數(shù)
login_and_buy_ticket('your_username', 'your_password', 'https://detail.damai.cn/item.htm?id=123456')
需要注意的:文章來源地址http://www.zghlxwxcb.cn/news/detail-509257.html
- 我用了
with
語句保證瀏覽器實(shí)例在函數(shù)結(jié)束后正確關(guān)閉。 - 我用了幾個(gè)
try-except
塊處理在各個(gè)步驟可能出現(xiàn)的異常,它可以在異常的時(shí)候打印出錯(cuò)誤的信息。 - 我把大部分代碼封裝在一個(gè)函數(shù)中,你如果需要,就可以輕輕松松再次調(diào)用該函數(shù)或者用作其他腳本的部分。
到了這里,關(guān)于基于Python和Selenium的大麥網(wǎng)自動(dòng)搶票腳本的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!