Python 3 Web Scraping Script to Find Alexa Global Rank of Website Using BeautifulSoup4 Library Full Project For Beginners

Python 3 Web Scraping Script to Find Alexa Global Rank of Website Using BeautifulSoup4 Library Full Project For Beginners

 

Welcome folks today in this blog post we will be finding alexa rank of website using beautifulsoup4 library. All the source code of the application is given below.

 

 

Get Started

 

 

In order to get started you need to install the following library using the pip command as shown below

 

pip install bs4

 

pip install urllib3

 

After installing these libraries you need to make an app.py file and copy paste the following code

 

app.py

 

#!/usr/bin/env python

from bs4 import BeautifulSoup
from urllib.request import urlopen
import sys


class AlexaRank:

    def parser(url):
        page = urlopen(url)
        html = page.read().decode("utf-8")
        soup = BeautifulSoup(html, "html.parser")
        return soup

    def cleaner(text):
        clean = text.replace("#", "")
        clean = clean.replace("\n", "")
        clean = clean.replace(" ", "")
        return clean

    def removewww(domain):
        name = domain.replace("http://", "")
        name = name.replace("https://", "")
        name = name.replace("www.", "")
        return name

    def globalrank(url):
        soup = AlexaRank.parser(url)
        text = soup.find("p", class_="big data").get_text()
        rank = AlexaRank.cleaner(text)
        return rank

    def countryrank(url):
        soup = AlexaRank.parser(url)
        soup = soup.find("div", id="CountryRank")
        soup = soup.find("li", )
        text = soup.find("span", class_="pull-right").get_text()
        rank = AlexaRank.cleaner(text)
        return rank


if __name__ == '__main__':
    try:
        domain = sys.argv[1]
    except IndexError:
        raise SystemExit(f"Usage: {sys.argv[0]} <domain_url>")
    if (domain == '-h') | (domain == '--help'):
        print(f'Alexa Rank Checker\nUsage: {sys.argv[0]} <domain_url>\nOptions:\n‑h, ‑‑help            show this help message and exit')
    else:
        domain = AlexaRank.removewww(domain)
        url = "https://www.alexa.com/siteinfo/" + domain
        try:
            print(AlexaRank.globalrank(url))
            print(AlexaRank.countryrank(url))
        except AttributeError:
            pass

 

See also  Python 3 TensorFlow Script to Add Dimension to Tensor and Add Values Full Example For Beginners

And now if you execute the above python script by typing the below command as shown below

 

python app.py domain_name

 

Where domain_name is the actual website that you want to check the alexa rank

 

python app.py codingshiksha.com

 

Now it will tell the alexa rank of codingshiksha.com

 

 

As you can see it has shown the alexa country and global rank of codingshiksha.com

Leave a Reply