Recently, I needed to use Google SEO (Search Engine Optimization) in my work. Friends who are familiar with SEO should know that one of the essential tasks is to check the search rankings of keywords. When there are only a few keywords, it's not a problem to check them one by one. However, in the later stages, a website may have hundreds or thousands of keywords, and it would take hours to check them individually.
Although there are many SEO tools available on the market, most of the free ones cannot perform batch queries. The maximum number of queries I have seen online is only 10 at a time, and the query speed is very slow.
Paid tools like Ahrefs and SEMrush charge a minimum of $995 per month. Of course, if you think it's suitable, you can purchase them, as many of these tools have useful features. Today, I will share with you a ranking search tool implemented in Python, and of course, it doesn't require any cost, just install the Python development environment.
Implementation Steps:
Without further ado, let's look at the code:
import requests
from bs4 import BeautifulSoup
First, we import two libraries: requests and BeautifulSoup. Requests is used to send HTTP requests, and BeautifulSoup is used to parse HTML.
def get_google_rank(keyword, website):
try:
url = f"https://www.google.com/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 # Return the ranking (starting from 1)
return -1 # If the website is not found, return -1
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None
The above code defines a function called get_google_rank
, which takes two parameters: keyword
(the keyword) and website
(the website domain). The goal of the function is to get the ranking of the specified keyword in Google search results.
Inside the function, we first construct a URL that uses the specified keyword for the Google search. Then, we set a User-Agent header to simulate a browser request. We use the requests.get
method to send an HTTP request and get the response of the search results page. The response.raise_for_status()
is used to check if the request was successful. If the returned status code is not 200, an exception will be raised.
Next, we use the BeautifulSoup library to parse the HTML content of the response. We create a BeautifulSoup object and parse it using the html.parser. Then, we use the find_all
method to find all the div elements with the class attribute 'g', which contain the information of the search results.
We then use the enumerate
function to iterate over the search results list and use result.find('a')['href']
to get the link of each search result. If the specified website domain appears in the link, we return the current ranking (starting from 1).
If the specified website domain is not found after the loop, the function returns -1 to indicate that the website was not found.
If an exception occurs during the request process, a requests.exceptions.RequestException
exception will be caught, and an error message will be printed, then None
will be returned.
# 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} has no ranking")
else:
print(f"{keyword} is ranked {rank}")
Finally, there is an example usage code. It defines a list of keywords called keywords
and a specified website domain called website
.
The code uses a for loop to iterate over the keywords list and calls the get_google_rank
function to get the ranking of each keyword in Google search results. If the returned ranking is not None, it checks the value of the ranking. If the ranking is -1, it prints a message indicating that the keyword has no ranking. Otherwise, it prints the ranking information of the keyword.
This is the meaning and logic of the entire code. The code allows you to get the ranking of a specified keyword in Google search results, and the example demonstrates how to use this function.
Complete 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 # Return the ranking (starting from 1)
return -1 # If the website is not found, return -1
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None
# 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} has no ranking")
else:
print(f"{keyword} is ranked {rank}")
Here are some screenshots of the search results on Bing: