Python Script for Retrieving Google Search Rankings Using Requests and BeautifulSoup
This article explains how to automate Google SEO keyword rank checking by writing a Python script that sends HTTP requests, parses the search results with BeautifulSoup, iterates through results to find a target domain, and handles errors, providing a free alternative to costly SEO tools.
When managing SEO for many keywords, manually checking Google rankings becomes time‑consuming, and many free tools limit batch queries while paid services like Ahrefs or SEMrush are expensive. The article presents a Python‑based solution that requires only a Python environment.
The script imports requests for HTTP requests and BeautifulSoup from bs4 for HTML parsing. It defines a function get_google_rank(keyword, website) that builds a Google search URL, sets a realistic User‑Agent header, fetches the page, and parses the HTML to locate all result containers with class g .
<code>import requests
from bs4 import BeautifulSoup
def get_google_rank(keyword, website):
try:
url = f"https://www.google.com.hk/search?q={keyword}"
headers = {'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36'}
response = requests.get(url, headers=headers)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
search_results = soup.find_all('div', class_='g')
for i, result in enumerate(search_results):
link = result.find('a')['href']
if website in link:
return i + 1 # ranking starts at 1
return -1 # not found
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None
</code>After defining the function, the article shows example usage: a list of keywords is created, a target website domain is set, and a loop calls get_google_rank for each keyword. Depending on the returned rank, it prints either a "no ranking" message or the rank number.
<code># Example usage
keywords = ['摸鱼小游戏', '是男人就下100层', '游戏']
website = 'haiyong.site'
for keyword in keywords:
rank = get_google_rank(keyword, website)
if rank is not None:
if rank == -1:
print(f"{keyword}没有排名")
else:
print(f"{keyword}排名第{rank}")
</code>The article concludes that this script provides a free, programmable way to obtain Google search rankings for multiple keywords, eliminating the need for costly third‑party SEO tools.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.