码迷,mamicode.com
首页 > 其他好文 > 详细

selenium - 截图下载图片

时间:2019-09-24 17:55:22      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:坐标   cli   save   tomat   captcha   top   nbsp   switch   doc   

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=rD:\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(rphoto.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(rphoto.png)
        picture = picture.crop((left_2, top_2, right_2, bottom_2))  # 二次截图:形成区块截图
        picture.save(rphoto3.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(rphoto.png)
        picture = picture.crop((left, top, right, bottom))  # 二次截图:形成区块截图
        picture.save(rphoto2.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()

 

selenium - 截图下载图片

标签:坐标   cli   save   tomat   captcha   top   nbsp   switch   doc   

原文地址:https://www.cnblogs.com/konghui/p/11579043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!