# https://www.228.com.cn/ticket-608506106.html
import base64
import os
import time
import urllib.request
import uuid
import requests
from PIL import Image
from aip import AipOcr
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.remote.webelement import WebElement
from 抢票 import damai, usernames
from 抢票.chaojiying import Chaojiying_Client
from 抢票.damai import PicTicket
baidu_dev = usernames.baidu
cjy = usernames.chaojiying
ocr_client = AipOcr(baidu_dev['appid'], baidu_dev['appkey'], baidu_dev['seckey'])
chaojiying = Chaojiying_Client(cjy['username'], cjy['password'], cjy['soft_id']) # 用户中心>>软件ID 生成一个替换 96001
class YongLeTicket(PicTicket):
# def __init__(self):
def ticket_page(self):
self.browser.get(self.ticket_url)
login_btn = self.wait_display('//a[@id="top-login-btn"]')
login_btn.click()
time.sleep(0.5)
username_input = self.wait_display('//input[@id="username"]')
password_input = self.browser.find_element_by_xpath('//input[@id="password"]')
username_input.send_keys(self.username)
password_input.send_keys(self.password)
submit_btn = self.browser.find_element_by_xpath('//input[@id="loginsubmit"]')
submit_btn.click()
time.sleep(1)
self.unlock()
# 买票
# 选择场次
rqcc_日期_场次 = self.browser.find_elements_by_xpath('//li[@type="date"]')
for item in rqcc_日期_场次:
if self.which_scene in item.text and item.get_attribute('class') != 'on':
item.click()
#选择价格
jg_价格 = self.browser.find_elements_by_xpath('//li[@type="price" and text()!=""]')
for item in jg_价格:
if self.which_price == item.get_attribute('zp') and item.get_attribute('class') != 'on' and item.text != '':
item.click()
#选择张数
##todo, 还不清楚永乐网抢票前的倒计时是什么样子的,可以参考大麦抢票的python ,待更新
#使用超级鹰识别图片进行点击
def unlock(self):
div = self.browser.find_elements_by_xpath('//div[@class="geetest_item_wrap"]')
while len(div) > 0:
# div = self.browser.find_element_by_xpath('//div[@class="geetest_tip_img"]')
img = self.browser.find_element_by_xpath('//img[@class="geetest_item_img"]')
# img.get_attribute('innerHTML')
# img.get_attribute('outerHTML')
img_url = img.get_property('src')
filename = str(uuid.uuid4()) + '.jpg'
try:
urllib.request.urlretrieve(img_url, filename)
ff = open(filename, 'rb')
im = ff.read()
fp = open(filename, mode='rb')
ii = Image.open(fp) # 返回一个Image对象
fp.close()
origin_width = ii.size[0]
origin_height = ii.size[1]
width_percent = origin_width / img.size['width']
height_percent = origin_height / img.size['height']
# {'err_no': 0, 'err_str': 'OK', 'pic_id': '9085915593272000012', 'pic_str': '141,214|168,79|284,206', 'md5': '1853206b097be4c617801989239d6584'}
info = chaojiying.PostPic(im, '9004')
ff.close()
finally:
os.remove(filename)
if info['err_no'] == 0:
positions = info['pic_str'].split('|')
for position in positions:
temp = position.split(',')
x_offset = int(temp[0])
y_offset = int(temp[1])
ActionChains(self.browser).move_to_element_with_offset(
to_element=img, xoffset=x_offset,
yoffset=y_offset).click().perform()
time.sleep(1)
self.browser.find_element_by_xpath('//div[@class="geetest_commit_tip"]').click()
time.sleep(1)
div = self.browser.find_elements_by_xpath('//div[@class="geetest_item_wrap"]')
def cut_img(self, ele: WebElement):
# 图片坐标
locations = ele.location
print(locations)
# 图片大小
sizes = ele.size
# 构造指数的位置
rangle = (int(locations['x']), int(locations['y']), int(locations['x'] + sizes['width']),
int(locations['y'] + sizes['height']))
save_path = str(uuid.uuid4()) + ".png"
driver.save_screenshot(save_path)
# 打开截图切割
img = Image.open(save_path)
jpg = img.convert('RGB')
jpg = img.crop(rangle)
path = save_path
jpg.save(path)
return path
if __name__ == '__main__':
try:
chromedriver_path = 'D:\crx\chromedriver.exe'
driver = webdriver.Chrome(chromedriver_path, )
driver.maximize_window()
yongle = YongLeTicket(driver, usernames.yongle[0]['username'], usernames.yongle[0]['password'], 'https://www.228.com.cn/ticket-608506106.html',
'香港',
'周五',
'1380元', 2, 0.01, 200)
yongle.ticket_page()
print(1)
finally:
driver.quit()