selenium-スクリーンショットダウンロード画像
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()
転載先:https://www.cnblogs.com/konghui/p/11579043.html