Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image scraping in Ruby

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

like image 804
Bhushan Lodha Avatar asked Sep 16 '26 04:09

Bhushan Lodha


2 Answers

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

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

like image 120
Phrogz Avatar answered Sep 17 '26 16:09

Phrogz


The lazy way is to use mechanize as it will figure out the urls and filenames for you:

require 'mechanize'
agent = Mechanize.new
doc = agent.get(url)
agent.get(doc.parser.at('.profilePic img')['src']).save
like image 39
pguardiario Avatar answered Sep 17 '26 18:09

pguardiario



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!