Pozycje keywordów w google z pomocą ruby

Przykładowy skrypt w ruby, który sprawdza pozycje w google dla keywordów odczytwanych z pliku.


#!/usr/bin/env ruby

require 'net/http'

class GoogleSearch

    def initialize(base_www)
        @base_www   = base_www.gsub(/http:\/\//,'')
        @google     = 'www.google.pl'
        @n          = 100
        @path       = "/search?num=#{@n}&hl=pl&q=&btnG=Szukaj&lr=lang_pl"
        @useragent  = 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.0.2) Gecko/20060111 Firefox/1.5.0.6a'
    end

    def position(keyword)
        @http = Net::HTTP.new(@google, 80)
        res = @http.get( @path.gsub(/&q=/,"&q=#{keyword.gsub(/\s+/,'+')}"), {'User-Agent' => @useragent})

        n = @n
        res.body.scan(/<!--m-->.*?<a href="(.*?)"/) { |url|
            n -= 1
            if url.to_s.gsub(/http:\/\//,'') =~ /^#{@base_www}/
                return [@n - n,url]
            end
        }

        return nil
    end
end

if __FILE__ == $0
    STDOUT.sync = true
    google = GoogleSearch.new('linuxlinki.md6.org')

    arr_kw = Array.new
    kw_file = open("keywords-google.txt")
    kw_file.each { |kw|
        kw.chomp!
        arr_kw.push(kw)
    }

    arr_kw.each do |kw|
        print "#{kw}: "
        if pos = google.position(kw)
            puts "#{pos[0]} #{pos[1]}"
        else
            puts "Not found"
        end
    end

end

Tags: ,

Leave a Reply