Skip to content

Instantly share code, notes, and snippets.

@puppe1990
Created March 22, 2023 16:24
Show Gist options
  • Save puppe1990/531d62bf236bcc4c70b150ec0ad7ad00 to your computer and use it in GitHub Desktop.
Save puppe1990/531d62bf236bcc4c70b150ec0ad7ad00 to your computer and use it in GitHub Desktop.
require 'httparty'
require 'nokogiri'
def google_rank_checker(domain, keyword)
url = "https://www.google.com/search?q=#{URI.encode_www_form_component(keyword)}&num=100"
headers = {
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Accept-Language' => 'en'
}
response = HTTParty.get(url, headers: headers)
parsed_html = Nokogiri::HTML(response.body)
rank = 0
found = false
parsed_html.css('.tF2Cxc').each_with_index do |result, index|
link = result.css('.yuRUbf a')[0]['href']
if link.include?(domain)
rank = index + 1
found = true
break
end
end
return found ? rank : nil
end
domain = 'example.com' # Replace with your domain
keyword = 'sample keyword' # Replace with your desired keyword
rank = google_rank_checker(domain, keyword)
if rank
puts "The domain #{domain} is ranked #{rank} for the keyword '#{keyword}'."
else
puts "The domain #{domain} is not found in the top 100 search results for the keyword '#{keyword}'."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment