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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#!/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 |
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