How to Get Google Page Ranking in Python?

Posted in /  

How to Get Google Page Ranking in Python?
vinaykhatri

Vinay Khatri
Last updated on April 20, 2024

    If you have a website and you wish to check the Google search ranking for a specific keyword for your website, then what options do you have? You can either search the keyword manually on the Google search bar and keep clicking on the next page until you see your website. Or, you can use a paid tool that can tell your web page ranking on a specific keyword.

    But if you are a Python developer and know how to use the Python Google library, you can create a Python program that can fetch you the Google rank number and page Number for your keyword.

    Here in this Python tutorial, I will walk you through a Python program that can get you the page ranking of your website on a specific keyword. But before diving into the program, let's install the required library.

    Install Library

    For this tutorial, we will be using the Python Google library to search for the keyword. And with some logic, we will compute the page no and index ranking of the keyword Post.

    As from the title, you know that this is a Python tutorial, so I am assuming that Python is installed in your System.

    Now run the following pip command on your terminal to install the Python Google library.

    pip install google

    Now we are all set. Open your best Python IDE or Text editor and start coding with us.

    Python Implementation

    Let's start with importing the Python Google library search module.

    from googlesearch import search
    import math

    Now ask the user to enter the keyword and website.

    # to search
    keyword =input("Enter the Search Keyword: ")
    my_website = input("Enter Your Website: ")

    Now we will use the imported search module and search for the keywords in Google.com. The search() function returns a list of all ranked URLs. So we can loop over those URLs and access them.

    # search for top 100 result
    urls = search(keyword, tld="com", num=100, stop=100, pause=2)

    Here,

    • The search() function will search the top 100 results for the keyword in Google.com . The keyword is the search query parameter.

    • The tld is the Top Level Domain, and here we have specified com that the search will be performed on Google.com. The tld value could also be in , co.in or any other Nation-level domain.
    • num specify the number of results we want, so here we have specified it 100 . This means the search() method will return 100 results.
    • The stop argument specifies where to stop the result. The pause argument defines the Lapse between HTTP requests.

    Now we have the top 100 URLs result. It's time to loop over those URLs and look for our website name, its ranking, and the page number.

    found = False
    
    for index, url in enumerate(urls):
        if my_website in url:
            print(f"Your Website Rank for keyword {keyword} is: {index+1}")
            print(f"And it displayed on Google Page Number:{math.ceil((index+1)/10)}")
            found = True
            break
    
    #if website in not in top 100 results
    if not found:
        print(f"Your Website is not in top 100 for keyword {keyword}")

    Now put all the code together and execute

    #Python program to Get Google Page Ranking

    from googlesearch import search
    import math
    
    # to search
    keyword =input("Enter the Search Keyword: ")
    my_website = input("Enter Your Website: ")
    
    # search for top 100 result
    urls = search(keyword, tld="com", num=100, stop=100, pause=2)
    
    found = False
    
    for index, url in enumerate(urls):
        if my_website in url:
            print(f"Your Website Rank for keyword {keyword} is: {index+1}")
            print(f"And it displayed on Google Page Number:{math.ceil((index+1)/10)}")
            found = True
            break
    
    if not found:
        print(f"Your Website is not in top 100 for keyword {keyword}")

    Output 1:

    Enter the Search Keyword: best django books
    Enter Your Website: techgeekbuzz.com
    Your Website Rank for keyword best django books is: 15
    And it displayed on Google Page Number:2

    Output 2:

    Enter the Search Keyword: best c++ online compilers
    Enter Your Website: techgeekbuzz.com
    Your Website Rank for keyword best c++ online compilers is: 1
    And it displayed on Google Page Number:1

    output 3

    Enter the Search Keyword: some random text
    Enter Your Website: techgeekbuzz.com
    Your Website is not in top 100 for keyword some random text

    Conclusion

    In this Python tutorial, we learned how to use the Python Google Library to find out the rank of Google rank of a Page on a specific keyword.

    In this tutorial, we only search for the top 100 results shown on the first 10 Google Pages. If you wish, you can also search for the top 200 or 500 search results.

    People are also reading:

    Leave a Comment on this Post

    0 Comments