Skip to content

Instantly share code, notes, and snippets.

Created January 18, 2015 19:46
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 anonymous/85d57f6f588b90f35c8a to your computer and use it in GitHub Desktop.
Save anonymous/85d57f6f588b90f35c8a to your computer and use it in GitHub Desktop.
#!/bin/env ruby
# encoding: utf-8
require 'open-uri'
require 'nokogiri'
require 'sequel'
require 'mysql2'
require 'pry'
##URL + Parse
url_planeo = "http://www.planeo.pt/"
dat_planeo = Nokogiri::HTML(open(url_planeo))
#DB connect + Dataset
DB = Sequel.mysql2('patinhas', :user => 'patinhas', :password => 'patinhas', :host => 'localhost')
dataset = DB[:ofertas]
planeo = dat_planeo.css('.panel-body')
planeo.each do | dat_planeo |
id = dat_planeo.at_css('.deal-image a')['href'].match( /(?:[^\/]*\/){4}([^\/]*)/ )[1].to_s
site = "Planeo.pt".to_s
camp = dat_planeo.at_css('.deal-title').text.gsub(/\t|\n|\€/,'').to_s
link = "http://www.planeo.pt"+dat_planeo.at_css('.deal-image a')['href']
imagem = dat_planeo.at_css('.deal-image a img')['src']
pvp = dat_planeo.at_css('.deal-price').text.gsub(/\t|\n|\€/,'')
pvr = dat_planeo.at_css('.deal-title s').text.gsub(/\t|\n|\€/,'')
percent = dat_planeo.at_css('.deal-discount').text.gsub('%','')
portes = 0
datetime= Date.parse(Time.now.to_s).strftime("%Y-%m-%d")
dataset.insert(:id => id, :site => site, :desc => camp, :link => link, :img => imagem, :pvp => pvp, :pvr => pvr, :percent => percent, :portes => portes, :data => datetime)
end
exeption
ruby sql_planeo.rb
sql_planeo.rb:22:in `block in <main>': undefined method `[]' for nil:NilClass (NoMethodError)
from /var/lib/gems/1.9.1/gems/nokogiri-1.6.3.1/lib/nokogiri/xml/node_set.rb:237:in `block in each'
from /var/lib/gems/1.9.1/gems/nokogiri-1.6.3.1/lib/nokogiri/xml/node_set.rb:236:in `upto'
from /var/lib/gems/1.9.1/gems/nokogiri-1.6.3.1/lib/nokogiri/xml/node_set.rb:236:in `each'
from sql_planeo.rb:21:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment