Last active
June 16, 2022 09:30
-
-
Save dimitryzub/6264c5f2e55e3704eae3fd3f55cda85c to your computer and use it in GitHub Desktop.
Google Rank Tracker in Python | Competitor Tracker
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
from serpapi import GoogleSearch | |
from datetime import date | |
import pandas as pd | |
import json | |
def google_website_ranker(): | |
keywords = pd.read_csv("sample-keywords.csv")["Keywords"].to_list() | |
target_websites = pd.read_csv("sample-keywords.csv")["Target-Website"].to_list() | |
website_ranking = [] | |
for index, (keyword, target_website) in enumerate(zip(keywords, target_websites), start=1): | |
# print(keyword, target_website) | |
params = { | |
"api_key": "your-serpapi-api-key", | |
"engine": "google", # search engine | |
"q": keyword, # search query | |
"google_domain": "google.com", # google domain | |
"gl": "us", # country of the search | |
"hl": "en", # language of the search | |
"num": "100", # number of results | |
"device": "desktop" # desktop, tablet, mobile | |
# other parameters | |
} | |
search = GoogleSearch(params) # where data extraction happens | |
results = search.get_dict() # JSON -> Python dict | |
if not "error" in results: | |
for result in results["organic_results"]: | |
if target_website in result["link"] and result["displayed_link"]: | |
website_ranking.append({ | |
"keyword": keyword, | |
"rank": f'{result["position"]} (URL{index}Rank)', | |
"target_website": f'{target_website} (URL{index})', | |
"device": params["device"], | |
"date": str(date.today()) | |
}) | |
else: | |
print(results["error"]) | |
print(json.dumps(website_ranking, indent=2, ensure_ascii=False)) | |
pd.DataFrame(data=website_ranking).to_csv(f"rank-tracking-{str(date.today())}.csv", index=False, encoding="utf-8") | |
if __name__ == "__main__": | |
google_website_ranker() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment