zsl-find

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()

 

分类:

技术点:

相关文章:

  • 2021-12-02
  • 2022-12-23
  • 2021-10-25
  • 2021-12-19
  • 2021-12-19
  • 2021-09-11
猜你喜欢
  • 2022-01-06
  • 2021-09-23
  • 2022-01-14
  • 2022-02-25
  • 2021-05-06
  • 2021-08-12
  • 2021-11-22
相关资源
相似解决方案