martes, 31 de marzo de 2009

Script con Ruby para el cálculo del KEI

Bien, lo prometido es deuda. Así que ahí va el código Ruby. Requiere de dos "gemas" la de Ruport (para presentar los datos de una manera bonita) y la de Hpricot (para hacer el captura de información). El código no es que sea super "power", pero es útil y sobre todo ayuda a conseguir los datos que uno necesita de forma automática.

Si a partir de aquí alguien es capaz de mejorarlo para obtener información mejor, ni falta hace que pida que se comparta.

Require 'rubygems'
require 'ruport'
require 'ruport/util'
require 'hpricot'
require 'open-uri'

# -------------------------------
# Set start time and end time
# -------------------------------
BEGIN {
$startTime = Time. Now
print "Inicio: #{$startTime}\n"
}

END {
$endTime = Time. Now
print "Fin: #{$endTime}\n"
print "Tiempo total: (#{$endTime - $startTime})"
}

# --------------------------------------------------
# DEFINITION'S SECTION
# --------------------------------------------------
sfInQuerys = 'C:\\temp\\SEO\\KW. Txt'
sfOutText = 'C:\\temp\\SEO\\OutKW. Txt'
sfOutCSV = 'C:\\temp\\SEO\\OutKW. Csv'

# --------------------------------------------------
# Definition of the "Resume" table that collects:
# + KW
# + allinanchor
# + intitle
# + inurl
# --------------------------------------------------
tblResume = Table(%w[KW raw allinanchor intitle inurl])


# --------------------------------------------------
# Input and Output Files
# --------------------------------------------------
# Input file: Reads a file an puts all lines in an aQuerys array
fIn = File. New(sfInQuerys,"r")
aQuerys = fIn. Readlines
fIn. Close

# Output file
fOut = File. New(sfOutText, 'w+') # the output file


shortSleep = 30
longSleep = 15

# --------------------------------------------------
# Start read KW's
# --------------------------------------------------
for i in 0.. AQuerys. Length-1

kw = aQuerys[i]. Gsub(' ', '+'). Chomp

# KW
sSearchString = "http://www.google.es/search? Hl=es&client=firefox-a&q=" + kw + "&btnG=Buscar"
puts sSearchString
#doc = Hpricot(open(sSearchString, :proxy => 'http://172.20.6. 5:4546')) # open web page
doc = Hpricot(open(sSearchString)) # open web page
htmlElements = doc. Search("//div[@id='ssb']/p/b")
raw = htmlElements[2]. InnerText
sleep(shortSleep)

# allinanchor
sSearchString = "http://www.google.es/search? Hl=es&client=firefox-a&q=allinanchor%3A" + kw + "&btnG=Buscar"
#doc = Hpricot(open(sSearchString, :proxy => 'http://172.20.6. 5:4546')) # open web page
doc = Hpricot(open(sSearchString)) # open web page
htmlElements = doc. Search("//div[@id='ssb']/p/b")
allinanchor = htmlElements[2]. InnerText
sleep(shortSleep)

# intitle
sSearchString = "http://www.google.es/search? Hl=es&client=firefox-a&q=intitle%3A" + kw + "&btnG=Buscar"
#doc = Hpricot(open(sSearchString, :proxy => 'http://172.20.6. 5:4546')) # open web page
doc = Hpricot(open(sSearchString)) # open web page
htmlElements = doc. Search("//div[@id='ssb']/p/b")
intitle = htmlElements[2]. InnerText
&nbs

No hay comentarios:

Publicar un comentario