1、splinter
>>> from splinter.browser import Browser
>>> b = Browser(driver_name="chrome")
>>> b.visit("http://www.qq.com")
但是,运行第二行时出错,
File "selenium\webdriver\common\service.py", line 81, in start
2、实现
## 抢票设计 2018.1.17
##rosefun
#参考:https://zhuanlan.zhihu.com/p/32928355
from splinter.browser import Browser
from time import sleep
import traceback
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import sys
sys.path.append(r'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe')
class Buy_Tickets(object):
# 定义实例属性,初始化
def __init__(self, username, passwd, order, passengers, dtime, starts, ends):
self.username = username
self.passwd = passwd
# 车次,0代表所有车次,依次从上到下,1代表所有车次,依次类推
self.order = order
# 乘客名
self.passengers = passengers
# 起始地和终点
self.starts = starts
self.ends = ends
# 日期
self.dtime = dtime
# self.xb = xb
# self.pz = pz
self.login_url = 'https://kyfw.12306.cn/otn/login/init'
self.initMy_url = 'https://kyfw.12306.cn/otn/index/initMy12306'
self.ticket_url = 'https://kyfw.12306.cn/otn/leftTicket/init'
self.driver_name = 'chrome'
# self.executable_path = 'C:\Python36\Scripts\chromedriver.exe'
self.executable_path = 'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe'
# 登录功能实现
def login(self):
self.driver.visit(self.login_url)
self.driver.fill('loginUserDTO.user_name', self.username)
# sleep(1)
self.driver.fill('userDTO.password', self.passwd)
# sleep(1)
print('请输入验证码...')
while True:
if self.driver.url != self.initMy_url:
sleep(1)
else:
break
# 买票功能实现
def start_buy(self):
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])
self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path)
#窗口大小的操作
self.driver.driver.set_window_size(700, 500)
self.login()
self.driver.visit(self.ticket_url)
try:
print('开始购票...')
# 加载查询信息
self.driver.cookies.add({"_jc_save_fromStation": self.starts})
self.driver.cookies.add({"_jc_save_toStation": self.ends})
self.driver.cookies.add({"_jc_save_fromDate": self.dtime})
self.driver.reload()
count = 0
if self.order != 0:
while self.driver.url == self.ticket_url:
self.driver.find_by_text('查询').click()
count += 1
print('第%d次点击查询...' % count)
try:
self.driver.find_by_text('预订')[self.order-1].click()
sleep(1.5)
except Exception as e:
print(e)
print('预订失败...')
continue
else:
while self.driver.url == self.ticket_url:
self.driver.find_by_text('查询').click()
count += 1
print('第%d次点击查询...' % count)
try:
for i in self.driver.find_by_text('预订'):
i.click()
sleep(1)
except Exception as e:
print(e)
print('预订失败...')
continue
print('开始预订...')
sleep(1)
print('开始选择用户...')
for p in self.passengers:
self.driver.find_by_text(p).last.click()
sleep(0.5)
if p[-1] == ')':
self.driver.find_by_id('dialog_xsertcj_ok').click()
print('提交订单...')
# sleep(1)
# self.driver.find_by_text(self.pz).click()
# sleep(1)
# self.driver.find_by_text(self.xb).click()
# sleep(1)
self.driver.find_by_id('submitOrder_id').click()
sleep(2)
print('确认选座...')
self.driver.find_by_id('qr_submit_id').click()
print('预订成功...')
except Exception as e:
print(e)
if __name__ == '__main__':
# 用户名
username = '****'
# 密码
password = '*****'
# 车次选择,0代表所有车次
order = 2
# 乘客名,比如passengers = ['丁小红', '丁小明']
# 学生票需注明,注明方式为:passengers = ['丁小红(学生)', '丁小明']
passengers = ['张三']
# 日期,格式为:'2018-01-20'
dtime = '2018-01-19'
# 出发地(需填写cookie值)
starts = '%u5434%u5821%2CWUY' #吴堡
# 目的地(需填写cookie值)
ends = '%u897F%u5B89%2CXAY' #西安
# xb =['硬座座']
# pz=['成人票']
# options = webdriver.ChromeOptions()
# # options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
# options.add_argument('test-type')
# browser = webdriver.Chrome(r'E:\QQBrowser\9.7.12672.400\QQBrowser.exe',chrome_options=options)
# browser.get("http://www.baidu.com/")
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-setuid-sandbox")
# 这里才是正式的使用了
# browser = Browser('chrome', options=chrome_options, executable_path=r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
# browser.visit('https://www.baidu.com')
Buy_Tickets(username, password, order, passengers, dtime, starts, ends).start_buy()
有个小问题:验证码需要自己输入,能不能进行图像识别,GitHub上应该有源代码。
参考: