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
| from selenium.webdriver import ActionChains from selenium import webdriver from time import sleep from selenium.webdriver.chrome.options import Options import requests import cv2
def init(): options = Options() options.binary_location = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" options.add_argument("-enable-webgl") options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") driver = webdriver.Chrome(options=options, executable_path=r'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe') driver.get('https://www.somd5.com/') return driver
def download(url,file): r = requests.get(url) with open(file, "wb") as code: code.write(r.content)
def identify_gap(bg,tp): ''' bg: 背景图片 tp: 缺口图片 ''' bg_img = cv2.imread(bg) tp_img = cv2.imread(tp) bg_edge = cv2.Canny(bg_img, 100, 200) tp_edge = cv2.Canny(tp_img, 100, 200) bg_pic = cv2.cvtColor(bg_edge, cv2.COLOR_GRAY2RGB) tp_pic = cv2.cvtColor(tp_edge, cv2.COLOR_GRAY2RGB) res = cv2.matchTemplate(bg_pic, tp_pic, cv2.TM_CCOEFF_NORMED) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) th, tw = tp_pic.shape[:2] tl = max_loc return tl[0] def search(driver,md5): username = driver.find_element_by_id('hash') username.clear() username.send_keys(md5) sleep(1)
btn = driver.find_element_by_id('TencentCaptcha') btn.click()
sleep(1)
frame = driver.find_element_by_xpath(xpath='//iframe')
sleep(2) driver.switch_to.frame(driver.find_element_by_xpath("/html/body/div[5]/iframe"))
test_button = driver.find_element_by_xpath('//*[@id="tcaptcha_drag_thumb"]')
big_img=driver.find_element_by_xpath('/html/body/div/div[3]/div[2]/div[1]/div[2]/img').get_attribute("src") small_img=driver.find_element_by_xpath('/html/body/div/div[3]/div[2]/div[1]/div[3]/img').get_attribute("src") download(big_img,'big_img.jpg') download(small_img,'small_img.png') action = ActionChains(driver) action.click_and_hold(on_element=test_button).perform() action.move_by_offset(xoffset=(identify_gap('big_img.jpg','small_img.png')-52)/2, yoffset=0).perform() test_button.click() sleep(6) result=driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/div[2]/div/div[2]').get_attribute("innerHTML") return result if __name__=='__main__': driver=init() f = open("md5.txt") line = f.readline() while line: result=search(driver,line) with open('result.txt','a+') as q: q.write(line.strip('\n')+"的查询结果:"+result+'\n') line = f.readline() f.close()
|