IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> selenium下载验证码图片 -> 正文阅读

[开发测试]selenium下载验证码图片

?有些验证码网站的图片链接是经过处理的,所以直接截图比较好,python截图代码如下:

from PIL import Image
#import pytesseract
from selenium import webdriver
 
# text = pytesseract.image_to_string(Image.open(r'C:\Users\windows7\Desktop\3.png'))
# print(text)
 
driver = webdriver.Chrome(executable_path=r'E:/python/chromedriver_win32/chromedriver.exe')
driver.get('http://img2.woyaogexing.com/2019/02/27/a4ff07bf5c2c47c5a015ac5adac4437f!600x600.jpeg')
driver.implicitly_wait(8)
 
#截全屏
# driver.get_screenshot_as_file('baidu.png')
 
driver.save_screenshot('full_baidu.png')
 
#只截取百度一下图标
img = driver.find_element_by_xpath('/html/body/img')
print(img.location)#{'x': 684, 'y': 301}
print(img.size)#{'height': 36, 'width': 100}
 
left = img.location['x']
top = img.location['y']
 
right = img.location['x'] + img.size['width']
bottom = img.location['y'] + img.size['height']
 
photo = Image.open('full_baidu.png')
photo = photo.crop((left, top, right, bottom))
 
photo.save('full_baidu.png')

scrapy完整代码如下:?

# -*- coding: utf-8 -*-
import scrapy
from selenium import webdriver
from selenium .webdriver .common .keys import  Keys
import time
import os
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from scrapy import signals
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from PIL import Image
class BasicdemoSpider(scrapy.Spider):
    name = 'basicdemo'
    #allowed_domains = ['www.baidu.com']
    start_urls = ['http://www.jinjianghotels.com/']
    def __init__(self):
        self.driver = webdriver.Chrome(executable_path=r'E:/python/chromedriver_win32/chromedriver.exe')
        self.wait = WebDriverWait(self.driver, 10)
        super().__init__()
    def parse(self, response):
        self.driver.maximize_window()
        self.driver.get('https://hotel.bestwehotel.com/NewLogin/?go=http%3A%2F%2Fwww.jinjianghotels.com%2F')
        time.sleep(8)
        #self.driver.find_element_by_xpath('/html/body/div[1]/div/div[2]/div/div/ul/li[2]').click()
        print(self.driver.page_source)  
        with open("fileName.txt","a+") as f:
            f.write(self.driver.page_source.encode('GBK','ignore').decode('GBK'))
            f.close()               
        self.driver.find_element_by_xpath('//*[@id="username"]').send_keys('12345678')
        self.driver.find_element_by_xpath('//*[@id="password"]').send_keys('666666')
        time.sleep(6)
        a=self.driver.find_element_by_xpath('//*[@id="imgVcode"]').get_attribute('src')
        print("这个获取的属性"+a)
        self.driver.get(a)
        time.sleep(6)
        self.driver.save_screenshot('full_baidu.png')
        img = self.driver.find_element_by_xpath('/html/body/img')
        print(img.location)#{'x': 684, 'y': 301}
        print(img.size)#{'height': 36, 'width': 100}
        left = img.location['x']
        top = img.location['y']
        right = img.location['x'] + img.size['width']
        bottom = img.location['y'] + img.size['height']
        photo = Image.open('full_baidu.png')
        photo = photo.crop((left, top, right, bottom)) 
        photo.save('full_baidu.png')
        #a=self.driver.find_element_by_xpath('/html/body/div[1]/div/div[2]/div/div/div[1]/form/div[1]/div[3]/img')
        #print(a.get_attribute('src')).encode(‘utf-8’). decode(‘utf-8’) 
#    def spider_closed(self, spider):
#        # 当爬虫退出的时候退出浏览器
#        print('spider closed')
#        self.driver.quit()
##下边的代码指的是官方给出的用signal自动关闭浏览器的方法
#    @classmethod
#    def from_crawler(cls, crawler, *args, **kwargs):
#       spider = super(BasicdemoSpider, cls).from_crawler(crawler, *args, **kwargs)
#       crawler.signals.connect(spider.closeSpider, signals.spider_closed)
#       return spider#
#    def closeSpider(self):
#       self.driver.quit()
#       print("代表运行")

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2022-04-18 18:12:40  更:2022-04-18 18:14:03 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 12:43:27-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码