Ruby 中的图像抓取 [英] Image scraping in Ruby

查看:57
本文介绍了Ruby 中的图像抓取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Nokogiri 抓取特定 URL 上的图像?如果有比 Nokogiri 更好的选择,请提出建议.css 图片标签是 .profilePic img

How do I scrape an image present on a particular URL using Nokogiri? If there are better options than Nokogiri please suggest. The css image tag is .profilePic img

推荐答案

如果只是一个带有 URL 的 :

If it is just an <img> with a URL:

PAGE = "http://site.com/page.html"
require 'nokogiri'
require 'open-uri'
html = Nokogiri.HTML(open(PAGE))
src  = html.at('.profilePic img')['src']
File.open("foo.png", "wb") do |f|
  f.write(open(src).read)
end

如果需要将相对图像路径转为绝对路径,请参见:
https://stackoverflow.com/a/4864170/405017

If you need to turn a relative image path into an absolute, see:
https://stackoverflow.com/a/4864170/405017

这篇关于Ruby 中的图像抓取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆