6

我需要将图像从 recaptcha 保存到 localhost 磁盘,我正在使用 watir-webdriver 获取图像 dom 元素,但它不支持保存方法,就像 watir 一样。那么如何将图像保存到我的磁盘?html:

<div id="recaptcha_image" style="width: 300px; height: 57px;">
  <img style="display:block;" alt="Проверка по слову reCAPTCHA" height="57" width="300" src="https://www.google.com/recaptcha/api/image?c=03AHJ_VusSUxF0IYURRcVTVTjJJnUk92j-hXYsuwqvu0m5tvKFzAnwvrHlz-j_Gfqg-sUrHLj3D2DrUYNNg4uvr2BNgZqlK5vpJUJVYkkWo36I4RRmRGkYZru5kBYhzPCCn49KlH6wW_iLw6vIzv7vnhpu6ndqxb-9SkIRrVYyBwN39kg18Lov7Hc">
</div>

和红宝石代码:

cap = @browsers[i].div(:id => 'recaptcha_image').image

如何将图像文件保存到磁盘?

4

3 回答 3

6
require 'watir-webdriver'    
require 'open-uri'

image_src = @browsers[i].div(:id => 'recaptcha_image').image.src

File.open("/path/", 'wb') do |f|
  f.write open(image_src).read
end
于 2013-01-08T16:48:33.077 回答
2

您可以按如下方式使用 Ruby 的open-uri

require 'open-uri'

url = "the image url" #https://www.google.com/recaptcha/api/image?c=03A....

File.open("./image.jpg", "wb") do |file_write|
  open(url, 'rb') do |file_read|
    file_write.write(file_read.read)
  end
end
于 2013-01-08T16:48:23.543 回答
0
    require 'watir-get-image-content'
    def save_captcha(@browser)      
        img = @browser.image(xpath: '//*[@id="ctl00_captcha"]/td[2]/div[1]/span[1]/img')        
        filename = "#{@path}/tmp/current_captcha.jpg"
        File.open(filename, 'wb'){|file| file.write( img.to_jpg) }          
    end
于 2017-02-04T01:28:08.200 回答