from selenium import webdriver from selenium.webdriver import ChromeOptions from time import sleep from PIL import Image import requests class Get_ahth(): """ 将验证码图片下到本地 """ def __init__(self): self.option = ChromeOptions() self.option.add_experimental_option('excludeSwitches', ['enable-automation']) self.bro = webdriver.Chrome(executable_path=r'D:chromedriver', chrome_options=self.option) self.bro.maximize_window() self.url='http://www.cn200.cc/member/my.php?mid=5&action=add' def get_img_all(self): bro = self.bro bro.get(self.url) js = "var q=document.documentElement.scrollTop=10000" bro.execute_script(js) bro.find_elements_by_id('answer')[0].click() bro.find_elements_by_id('captcha')[0].click() sleep(5) bro.save_screenshot(r'photo.png') # 一次截图:形成全图 def get_img_num(self): bro = self.bro img_yz_2 = bro.find_elements_by_id('question')[0] left_2 = img_yz_2.location['x'] # 区块截图左上角在网页中的x坐标 top_2 = 778 # 区块截图左上角在网页中的y坐标 right_2 = left_2 + img_yz_2.size['width'] - 65 # 区块截图右下角在网页中的x坐标 bottom_2 = top_2 + img_yz_2.size['height'] + 14 # 区块截图右下角在网页中的y坐标 print({"left": left_2, "top": top_2, "right": right_2, "bottom ": bottom_2}) picture = Image.open(r'photo.png') picture = picture.crop((left_2, top_2, right_2, bottom_2)) # 二次截图:形成区块截图 picture.save(r'photo3.png') def get_img_img(self): bro = self.bro img_yz = bro.find_elements_by_id('captchapng')[0] left = img_yz.location['x'] # 区块截图左上角在网页中的x坐标 top = 835 # 区块截图左上角在网页中的y坐标 right = left + img_yz.size['width'] # 区块截图右下角在网页中的x坐标 bottom = top + img_yz.size['height'] + 14 # 区块截图右下角在网页中的y坐标 print({"left": left, "top": top, "right": right, "bottom ": bottom}) picture = Image.open(r'photo.png') picture = picture.crop((left, top, right, bottom)) # 二次截图:形成区块截图 picture.save(r'photo2.png') def close(self): self.bro.quit() def main(self): self.get_img_all() self.get_img_num() self.get_img_img() self.close() gg = Get_ahth() gg.main()