Last active
December 29, 2015 09:39
-
-
Save murparreira/7651466 to your computer and use it in GitHub Desktop.
Unfinished script that gets pokemon data from pokemondb.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' | |
class Pokemon | |
attr_accessor :name, :moves, :abilities | |
def initialize | |
@name = '' | |
@moves = [] | |
@abilities = [] | |
end | |
end | |
pokemons = [] | |
agent = Mechanize.new | |
puts "Visiting main page." | |
all_page = agent.get("http://pokemondb.net/pokedex/all") | |
noko = Nokogiri::HTML(all_page.body) | |
links = noko.css('.ent-name').to_a | |
links.each do |a| | |
pkmn_page = all_page.link_with(:href => a.attr('href')).click | |
noko = Nokogiri::HTML(pkmn_page.body) | |
pokemon = Pokemon.new | |
name_tag = noko.at_css('.main-content h1') | |
name = name_tag.text if name_tag | |
pokemon.name = name | |
puts "Visiting #{name} page." | |
puts "Getting #{name} abilities." | |
abilities_table = noko.css('.vitals-table').first | |
if abilities_table | |
abilities_links = abilities_table.css('tbody tr:nth-child(6) a') | |
abilities_links.each do |a| | |
pokemon.abilities << a.text if a | |
end | |
end | |
puts "Getting #{name} moveset." | |
moveset_table = noko.css('.tabset-moves-form-game .svtabs-panel-list li:first .data-table tbody tr a.ent-name') | |
moveset_table.each do |m| | |
pokemon.moves << m.text if m | |
end | |
pokemons << pokemon | |
puts pokemon.inspect | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment