1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| import re import time import random import requests from PIL import Image from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from io import BytesIO from selenium.webdriver.common.action_chains import ActionChains
def get_merge_img(img_content,location_list,num): ''' 拼接图片 :param img_content: :param location_list: :param num: :return: ''' im = Image.open(img_content) im_list_upper = [] im_list_done = [] for location in location_list: if int(location['y']) == -58: im_list_upper.append(im.crop((abs(int(location['x'])),58,abs(int(location['x']))+10,116))) if int(location['y']) == 0: im_list_done.append(im.crop((abs(int(location['x'])),0,abs(int(location['x']))+10,58)))
new_im = Image.new('RGB',(260,116)) x_offset=0 for im in im_list_upper: new_im.paste(im,(x_offset,0)) x_offset +=10
x_offset = 0 for im in im_list_done: new_im.paste(im, (x_offset, 58)) x_offset += 10
return new_im
def get_img(driver,div_class,num): ''' 获取图片 :param driver: :param div_class: :param num: :return: ''' background_imgs = driver.find_elements_by_class_name(div_class) location_list = [] imge_url = '' for img in background_imgs:
location = {} imge_url = re.findall(r'background-image: url\(\"(.*?)\"\); background-position: (.*?)px (.*?)px;',img.get_attribute('style'))[0][0] location['x'] = re.findall(r'background-image: url\(\"(.*?)\"\); background-position: (.*?)px (.*?)px;',img.get_attribute('style'))[0][1] location['y'] = re.findall(r'background-image: url\(\"(.*?)\"\); background-position: (.*?)px (.*?)px;',img.get_attribute('style'))[0][2]
location_list.append(location)
response = requests.get(imge_url).content img_content = BytesIO(response)
image = get_merge_img(img_content,location_list,num) image.save('{}.jpg'.format(num)) return image
def get_diff_location(image1,image2): ''' 通过像素对比 找到缺口位置 :param image1: :param image2: :return: ''' for x in range(1,259): for y in range(1, 115): if is_similar(image1,image2,x,y) == False: return x
def is_similar(image1,image2,x,y): pixel1 = image1.getpixel((x,y)) pixel2 = image2.getpixel((x,y))
for i in range(0,3): if abs(pixel1[i]) - pixel2[i] >=50: return False return True
def get_track(x): ''' 滑块移动轨迹 初速度 v =0 单位时间 t = 0.2 位移轨迹 tracks = [] 当前位移 ccurrent = 0 :param x: :return: ''' v = 0 t = 0.2 tracks = [] current = 0 while current < x: a = 2 v0 = v s =v0*t+0.5*a*(t**2) current = current+s tracks.append(round(s)) v = v0+a*t
for i in range(3): tracks.append(-1) for i in range(3): tracks.append(-2) return tracks
def main(driver,element):
image1 = get_img(driver,'gt_cut_fullbg_slice',1) image2 = get_img(driver,'gt_cut_bg_slice',2)
x = get_diff_location(image1,image2) tracks = get_track(x) ActionChains(driver).click_and_hold(element).perform() for x in tracks: ActionChains(driver).move_by_offset(xoffset=x,yoffset=0).perform() ActionChains(driver).release(element).perform() time.sleep(3)
if __name__ == '__main__': driver = webdriver.Firefox() driver.maximize_window() driver.get('http://www.cnbaowen.net/api/geetest/') try: count = 5 wait = WebDriverWait(driver, 10) element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'gt_slider_knob'))) while count >0: main(driver,element) try: succes = wait.until(EC.presence_of_all_elements_located((By.XPATH,'//div[@class="gt_ajax_tip gt_success"]'))) if succes: print('恭喜你!识别成功...') break except Exception as e: print('识别错误,继续') count -=1 finally: driver.quit()
|