Created
March 22, 2023 16:24
-
-
Save puppe1990/531d62bf236bcc4c70b150ec0ad7ad00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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