Last active
December 29, 2015 08:09
-
-
Save murparreira/7641444 to your computer and use it in GitHub Desktop.
Little script to download all new pokemon images from serebii.net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'mechanize' | |
types = ['bug', 'dark', 'dragon', 'electric', 'fairy', 'fighting', 'fire', 'flying', 'ghost', 'grass', 'ground', 'ice', 'normal', 'poison', 'psychic', 'rock', 'steel', 'water'] | |
FOLDER_PATH = "/path/to/your/running/script/" | |
types.each do |type| | |
array = [] | |
agent = Mechanize.new | |
puts "Visiting #{type} page!" | |
type_page = agent.get("http://www.serebii.net/pokedex-xy/#{type}.shtml") | |
noko = Nokogiri::HTML(type_page.body) | |
table = noko.css('.dextable tr') | |
puts "Found approximately #{table.count} pkmns!" | |
table.each do |t| | |
el = t.at_css('td:nth-child(3) a') | |
array << el.attr('href') if el | |
end | |
array.each do |a| | |
pkmn_page = type_page.link_with(:href => a).click | |
noko = Nokogiri::HTML(pkmn_page.body) | |
pkmn_name = noko.at_css('font b').text[5..-1] | |
puts "Visiting #{pkmn_name} page!" | |
img = noko.at_css('.dextable img').attr('src') | |
agent.get(img).save FOLDER_PATH+pkmn_name | |
puts "Saving #{pkmn_name} image!" | |
end | |
end | |
puts "Removing extra images!" | |
Dir.foreach(FOLDER_PATH) do |img| | |
File.delete(FOLDER_PATH+img) unless img.match(/\.\d+/).nil? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment