py分布式搶票
https://github.com/pjialin/py12306/blob/master/README.md
升級py
https://www.cnblogs.com/youngthink/p/6668127.html
https://www.jb51.net/article/132629.htm
https://www.jb51.net/article/75992.htm
https://blog.csdn.net/a2011480169/article/details/67141540
python -m pip install --upgrade pip
pip install splinter -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
視頻教程
https://www.bilibili.com/video/av18202461/?p=6
pip install selenium
命令教程
https://www.cnblogs.com/hellosecretgarden/p/9206648.html
搶票教程
https://www.cnblogs.com/Klay/p/9643946.html
瀏覽器下載
https://dl.pconline.com.cn/download/51614-1.html
chromedriver下載
http://npm.taobao.org/mirrors/chromedriver/
import selenium
from selenium import webdriver # 導入webdriver模塊
chrome_obj = webdriver.Chrome() # 打開Google瀏覽器
chrome_obj.get("https://www.baidu.com") # 打開 網址
chrome_obj.title # 獲取打開網址 的名字
chrome_obj.current_url # 獲取打開網址的 url<br><br>>
chrome_obj.close() #關閉瀏覽器窗口
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
def wait_response_time(chrome,waittime,func):
# 返回 func執行結果
return WebDriverWait(chrome,waittime).until(func)
def automatic_login(name,pwd,url):
chrome = webdriver.Chrome()
chrome.get(url)
time.sleep(2)
chrome.maximize_window()
# time.sleep(2)
# chrome.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div").click()
############## 第二種方法 設置時間延遲
# login_btn_lable = wait_response_time(chrome,5,
# lambda chrome: chrome.find_element_by_xpath("/html/body/div[3]/div[2]/div[3]/div/div"))
# login_btn_lable.click() ## 利用函數設置等待響應時間
#chrome.find_element_by_link_text("賬號登錄").click()
chrome.find_element_by_xpath("/html/body/div[2]/div[2]/ul/li[2]/a").click()
time.sleep(1)
name_label = chrome.find_element_by_id("J-userName")
name_label.send_keys(" ") # 防止發送不成功
name_label.clear()
name_label.send_keys(name)
pwd_label = chrome.find_element_by_id("J-password")
pwd_label.clear()
pwd_label.send_keys(pwd)
print ("等待驗證碼,自行輸入...")
time.sleep(10)
time.sleep(10)
chrome.find_element_by_id("chepiaodingdan").click()
time.sleep(10)
xf = chrome.find_element_by_link_text("車票")
ActionChains(chrome).move_to_element(xf).perform() # 模擬用戶懸浮
chrome.find_element_by_css_selector("#J-chepiao > div > div:nth-child(1) > ul > li.nav_dan > a").click()
#chrome.close()
time.sleep(10)
cookies1 = {'name':'_jc_save_fromStation','value':'%u9E64%u58C1%2CHAF'}
cookies2 = {'name':'_jc_save_toStation','value':'%u90D1%u5DDE%2CZZF'}
cookies3 = {'name':'_jc_save_fromDate','value':'2019-02-05'}
chrome.add_cookie(cookie_dict=cookies1)
chrome.add_cookie(cookie_dict=cookies2)
chrome.add_cookie(cookie_dict=cookies3)
time.sleep(10)
chrome.refresh()
count=0
order=3
chrome.find_element_by_link_text("查詢").click()
chrome.find_element_by_link_text("預訂")[3].click()
print (u"還沒開始預訂")
print (u"開始預訂...")
if __name__ == "__main__":
name = "xx"
pwd = "xx"
url = "https://kyfw.12306.cn/otn/resources/login.html"
automatic_login(name,pwd,url)
from selenium import webdriver import time from selenium.webdriver.common.action_chains import ActionChains def main(): login_text() search_ticket() book_ticket() def login_text(): d.get(url) d.implicitly_wait(30) #登陸賬號 username_ele = d.find_element_by_id('username') username_ele.clear() username_ele.send_keys(username) #登陸密碼 pwd_ele = d.find_element_by_id('password') pwd_ele.clear() pwd_ele.send_keys(pwd) while True: #手動進行圖片驗證,並登錄 curpage_url = d.current_url if curpage_url != url: if curpage_url[:-1] != url: print('.......登陸成功........') break else: time.sleep(3) print('--------->等待用戶進行圖片驗證') def search_ticket(): time.sleep(10) d.find_element_by_id("chepiaodingdan").click() time.sleep(10) xf = d.find_element_by_link_text("車票") ActionChains(d).move_to_element(xf).perform() # 模擬用戶懸浮 d.find_element_by_css_selector("#J-chepiao > div > div:nth-child(1) > ul > li.nav_dan > a").click() time.sleep(3) #輸入出發地 d.add_cookie({'name':'_jc_save_fromStation','value':'%u9E64%u58C1%2CHAF'}) #深圳 #選擇目的地 d.add_cookie({'name':'_jc_save_toStation','value':'%u90D1%u5DDE%2CZZF'}) #衡陽 # %u5E7F%u5DDE%u5357%2CIZQ(廣州南) %u8861%u9633%2CHYQ(衡陽) %u6DF1%u5733%2CSZQ(深圳) #選擇出發日期 d.add_cookie({'name':'_jc_save_fromDate','value':'2019-02-05'}) d.refresh() time.sleep(10) def book_ticket(): query_time = 0 time_begin = time.time() #循環查詢 while True: time.sleep(1) search_btn = d.find_element_by_link_text('查詢') search_btn.click() #掃描查詢結果 //*[@id="WZ_250000K25711"] #無座//*[@id="YZ_250000K25711"]#硬卧 try: d.implicitly_wait(10) #深圳到廣州南G1010的PATH:('//*[@id="ZE_6i000G10100C"]) 深圳到衡陽G822:('//*[@id="ZE_6i0000G8220H"]') ticket_ele = d.find_element_by_xpath('//*[@id="YZ_250000K25711"]') #所搶車次對應的座位等級的xpath ticket_info = ticket_ele.text except: search_btn.click() d.implicitly_wait(5) ticket_ele = d.find_element_by_xpath('//*[@id="YZ_250000K25711"]') ticket_info = ticket_ele.text print('可能您的xpath選擇錯誤') if ticket_info == '無' or ticket_info == '*': query_time += 1 cur_time = time.time() print('第%d次查詢,用時%s秒'%(query_time,cur_time - time_begin)) else: d.find_element_by_xpath('//*[@id="ticket_250000K25711"]/td[13]/a').click() break cust_url = 'https://kyfw.12306.cn/otn/confirmPassenger/initDc' while True: if (d.current_url == cust_url): print('頁面跳至選擇乘客信息 成功') break else: time.sleep(1) print('等待頁面跳轉') while True: try: d.find_element_by_xpath('//*[@id="normalPassenger_0"]').click() #_0是聯系人列表里的第一個 ,依此類推 break except: print('等待常用聯系人列表') #提交訂單 d.find_element_by_xpath('//*[@id="submitOrder_id"]').click() #確認訂票信息 while True: try: d.switch_to.frame(d.find_element_by_xpath('/html/body/iframe[2]')) print d.find_element_by_xpath('//*[@id="qr_submit_id"]') print('pass') except: print('請手動選座和點擊確認信息') time.sleep(5) break if __name__ == '__main__': d = webdriver.Chrome() url = 'https://kyfw.12306.cn/otn/login/init' username= 'xx' pwd = 'xx' main()
