3

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

Mark Kadlec
  • 8,036
  • 17
  • 60
  • 96
Bhushan Lodha
  • 6,824
  • 7
  • 62
  • 100

2 Answers2

10

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

Community
  • 1
  • 1
Phrogz
  • 296,393
  • 112
  • 651
  • 745
2

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
pguardiario
  • 53,827
  • 19
  • 119
  • 159