Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ruby&;获取图像文件;水豚_Ruby_Selenium_Webkit_Capybara_Capybara Webkit - Fatal编程技术网

使用ruby&;获取图像文件;水豚

使用ruby&;获取图像文件;水豚,ruby,selenium,webkit,capybara,capybara-webkit,Ruby,Selenium,Webkit,Capybara,Capybara Webkit,它是capybara通过HTTPS协议访问的页面上的图像标签: <img src="path"> 如果我没弄错的话,你可能想要的是Ruby的HTTPS请求。尝试: require 'net/https' url = URI.parse('path') Net::HTTP.start(url.host, url.port, :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|

它是capybara通过HTTPS协议访问的页面上的图像标签:

<img src="path">

如果我没弄错的话,你可能想要的是Ruby的HTTPS请求。尝试:

require 'net/https'

url = URI.parse('path')

Net::HTTP.start(url.host, url.port, :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
  res = http.get(url.request_uri)
  open("image.png", "wb") do |f|
    f.write(res.body)
  end
end
对于裁剪,可以使用(纯Ruby)或(需要ImageMagick)

编辑:如果您想遵循重定向,您可以这样做

require 'net/https'

def process_image( content )
  # do your cropping here

  open("image.png", "wb") do |f|
    f.write(content)
  end
end

def fetch( url )
  Net::HTTP.start(url.host, url.port, :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
    response = http.get(url.request_uri)
    case response.code
    when Net::HTTPRedirection
      fetch response['location']
    else
      process_image response.body
    end
  end
end

fetch URI.parse('path')

证书验证失败。不知道为什么。据我所知,当解释器进入块时,到该服务器的连接就建立了,所有存储的会话cookie都将在执行http.get时发送。不管怎样,你为我指明了正确的搜索方向。谢谢我猜您使用的是Ruby 1.9,其中的证书验证比以前的版本更严格。您可以使用
:verify\u mode=>OpenSSL::SSL::verify\u NONE
解决此问题。我也更新了我的答案。是的,你是对的!但响应是:
对象已移动对象已移动到这与我之前的wget和curl结果类似。这可能意味着,您的浏览器正在重定向您。您可以尝试在命令行中键入类似于Please的
curl-I
,并显示结果,以便我可以帮助您。
require 'net/https'

url = URI.parse('path')

Net::HTTP.start(url.host, url.port, :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
  res = http.get(url.request_uri)
  open("image.png", "wb") do |f|
    f.write(res.body)
  end
end
require 'net/https'

def process_image( content )
  # do your cropping here

  open("image.png", "wb") do |f|
    f.write(content)
  end
end

def fetch( url )
  Net::HTTP.start(url.host, url.port, :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
    response = http.get(url.request_uri)
    case response.code
    when Net::HTTPRedirection
      fetch response['location']
    else
      process_image response.body
    end
  end
end

fetch URI.parse('path')