Skip to content

Instantly share code, notes, and snippets.

@dreamingechoes
Last active March 7, 2016 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreamingechoes/90f434ce897d200e9dba to your computer and use it in GitHub Desktop.
Save dreamingechoes/90f434ce897d200e9dba to your computer and use it in GitHub Desktop.
A simple test of web scraping to fetch Pokemons data from pokemondb.net
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
require 'open-uri'
base_url = "http://pokemondb.net"
url_index = "#{base_url}/pokedex/game/firered-leafgreen"
index = Nokogiri::HTML(open(url_index))
index.css(".infocard-tall").each do |item|
begin
name = item.at_css(".ent-name").text
puts "Fetching #{name} info..."
url_detail = "#{base_url}#{item.at_css(".ent-name")[:href]}"
number = kind = species = height = weight = abilities = nil
pokemon_detail = Nokogiri::HTML(open(url_detail))
number = pokemon_detail.at_css(".vitals-table tr:contains('National')").at_css("td").text
kind = pokemon_detail.at_css(".vitals-table tr:contains('Type')").at_css("td").text.split(" ").join(", ")
species = pokemon_detail.at_css(".vitals-table tr:contains('Species')").at_css("td").text
height = pokemon_detail.at_css(".vitals-table tr:contains('Height')").at_css("td").text
weight = pokemon_detail.at_css(".vitals-table tr:contains('Weight')").at_css("td").text
abilities = pokemon_detail.at_css(".vitals-table tr:contains('Abilities')").at_css("td")
rescue
puts "Something goes wrong with #{name} :("
ensure
puts "Pokemon info"
puts "Name: #{name}, number: #{number}, kind: #{kind}, species: #{species}, height: #{height}, weight: #{weight}, abilities: #{abilities}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment