Class: SeoKeywordsGenerator::Scraper
- Inherits:
-
Object
- Object
- SeoKeywordsGenerator::Scraper
- Defined in:
- lib/seo-keywords-generator-ruby.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#country ⇒ Object
Returns the value of attribute country.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#lang ⇒ Object
Returns the value of attribute lang.
-
#query ⇒ Object
Returns the value of attribute query.
Instance Method Summary collapse
- #get_auto_complete ⇒ Object
- #get_depth_results(token, depth) ⇒ Object
- #get_related_questions(depth_limit = 0) ⇒ Object
- #get_related_searches ⇒ Object
-
#initialize(query, api_key, lang = 'en', country = 'us', domain = 'google.com') ⇒ Scraper
constructor
A new instance of Scraper.
- #print_data(data) ⇒ Object
- #save_to_csv(data) ⇒ Object
- #save_to_json(data) ⇒ Object
- #save_to_txt(data) ⇒ Object
Constructor Details
#initialize(query, api_key, lang = 'en', country = 'us', domain = 'google.com') ⇒ Scraper
Returns a new instance of Scraper.
11 12 13 14 15 16 17 18 |
# File 'lib/seo-keywords-generator-ruby.rb', line 11 def initialize(query, api_key, lang = 'en', country = 'us', domain = 'google.com') @query = query @api_key = api_key @lang = lang @country = country @domain = domain = [] end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
9 10 11 |
# File 'lib/seo-keywords-generator-ruby.rb', line 9 def api_key @api_key end |
#country ⇒ Object
Returns the value of attribute country.
9 10 11 |
# File 'lib/seo-keywords-generator-ruby.rb', line 9 def country @country end |
#domain ⇒ Object
Returns the value of attribute domain.
9 10 11 |
# File 'lib/seo-keywords-generator-ruby.rb', line 9 def domain @domain end |
#lang ⇒ Object
Returns the value of attribute lang.
9 10 11 |
# File 'lib/seo-keywords-generator-ruby.rb', line 9 def lang @lang end |
#query ⇒ Object
Returns the value of attribute query.
9 10 11 |
# File 'lib/seo-keywords-generator-ruby.rb', line 9 def query @query end |
Instance Method Details
#get_auto_complete ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/seo-keywords-generator-ruby.rb', line 20 def get_auto_complete params = { api_key: @api_key, # https://serpapi.com/manage-api-key engine: 'google_autocomplete', # search engine q: @query, # search query gl: @country, # country of the search hl: @lang # language of the search } search = GoogleSearch.new(params) # data extraction on the SerpApi backend results = search.get_hash # JSON -> Ruby hash results[:suggestions].map{ |result| result[:value] }.compact end |
#get_depth_results(token, depth) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/seo-keywords-generator-ruby.rb', line 51 def get_depth_results(token, depth) depth_params = { q: @query, api_key: @api_key, engine: 'google_related_questions', next_page_token: token } depth_search = GoogleSearch.new(depth_params) depth_results = depth_search.get_hash += depth_results[:related_questions]&.map{ |result| result[:question] } if depth > 1 depth_results[:related_questions]&.each do |question| if question[:next_page_token] get_depth_results(question[:next_page_token], depth - 1) end end end end |
#get_related_questions(depth_limit = 0) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/seo-keywords-generator-ruby.rb', line 73 def (depth_limit = 0) params = { api_key: @api_key, # https://serpapi.com/manage-api-key engine: 'google', # search engine q: @query, # search query google_domain: @domain, # Google domain to use gl: @country, # country of the search hl: @lang # language of the search } search = GoogleSearch.new(params) # data extraction on the SerpApi backend results = search.get_hash # JSON -> Ruby hash = results[:related_questions]&.map{ |result| result[:question] } depth_limit = 4 if depth_limit > 4 if depth_limit > 0 results[:related_questions]&.each do |question| if question[:next_page_token] get_depth_results(question[:next_page_token], depth_limit) end end end end |
#get_related_searches ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/seo-keywords-generator-ruby.rb', line 35 def params = { api_key: @api_key, # https://serpapi.com/manage-api-key engine: 'google', # search engine q: @query, # search query google_domain: @domain, # Google domain to use gl: @country, # country of the search hl: @lang # language of the search } search = GoogleSearch.new(params) # data extraction on the SerpApi backend results = search.get_hash # JSON -> Ruby hash results[:related_searches].map{ |result| result[:query] }.compact end |
#print_data(data) ⇒ Object
128 129 130 |
# File 'lib/seo-keywords-generator-ruby.rb', line 128 def print_data(data) puts JSON.pretty_generate(data) end |
#save_to_csv(data) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/seo-keywords-generator-ruby.rb', line 101 def save_to_csv(data) CSV.open("#{@query.gsub(' ', '_')}.csv", 'w') do |csv_file| csv_file << data.keys max_length = data.values.map(&:length).max (0...max_length).each do |index| csv_file << data.values.map { |value| value[index] } end end end |
#save_to_json(data) ⇒ Object
111 112 113 114 115 |
# File 'lib/seo-keywords-generator-ruby.rb', line 111 def save_to_json(data) File.open("#{@query.gsub(' ', '_')}.json", 'w') do |json_file| json_file.write(JSON.pretty_generate(data)) end end |
#save_to_txt(data) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/seo-keywords-generator-ruby.rb', line 117 def save_to_txt(data) File.open("#{@query.gsub(' ', '_')}.txt", "w") do |txt_file| data.each do |key, values| txt_file.puts("#{key}:") values.each do |value| txt_file.puts(" #{value}") end end end end |