Class: SeoKeywordsGenerator::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/seo-keywords-generator-ruby.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, api_key, lang = 'en', country = 'us', domain = 'google.com') ⇒ Scraper

Returns a new instance of Scraper.



101
102
103
104
105
106
107
108
# File 'lib/seo-keywords-generator-ruby.rb', line 101

def initialize(query, api_key, lang = 'en', country = 'us', domain = 'google.com')
    @query = query
    @api_key = api_key
    @lang = lang
    @country = country
    @domain = domain
    @related_questions_results = []
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



99
100
101
# File 'lib/seo-keywords-generator-ruby.rb', line 99

def api_key
  @api_key
end

#countryObject

Returns the value of attribute country.



99
100
101
# File 'lib/seo-keywords-generator-ruby.rb', line 99

def country
  @country
end

#domainObject

Returns the value of attribute domain.



99
100
101
# File 'lib/seo-keywords-generator-ruby.rb', line 99

def domain
  @domain
end

#langObject

Returns the value of attribute lang.



99
100
101
# File 'lib/seo-keywords-generator-ruby.rb', line 99

def lang
  @lang
end

#queryObject

Returns the value of attribute query.



99
100
101
# File 'lib/seo-keywords-generator-ruby.rb', line 99

def query
  @query
end

Instance Method Details

#get_auto_completeObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/seo-keywords-generator-ruby.rb', line 110

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/seo-keywords-generator-ruby.rb', line 141

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
    
    @related_questions_results += 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


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/seo-keywords-generator-ruby.rb', line 163

def get_related_questions(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
    
    @related_questions_results = 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

    @related_questions_results
end


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/seo-keywords-generator-ruby.rb', line 125

def get_related_searches
    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


218
219
220
# File 'lib/seo-keywords-generator-ruby.rb', line 218

def print_data(data)
    puts JSON.pretty_generate(data)
end

#save_to_csv(data) ⇒ Object



191
192
193
194
195
196
197
198
199
# File 'lib/seo-keywords-generator-ruby.rb', line 191

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



201
202
203
204
205
# File 'lib/seo-keywords-generator-ruby.rb', line 201

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



207
208
209
210
211
212
213
214
215
216
# File 'lib/seo-keywords-generator-ruby.rb', line 207

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