It is very simple to develop a query search ranking tool in Python

For those who are learning to communicate and looking for source codes, please click 【25 Python Practical Project Source Codes

Build the background

Recently, I need to use Google SEOQ (search engine optimization) in my work. Friends who have known it should know that one of the indispensable tasks of SEO is to query the search ranking of keywords. When there are few keywords, you can check them one by one without any problem, but in the later stage, a website has hundreds or even thousands of keywords, and it will take at least several hours for you to check each person one by one.

Although there are many SEO free or paid tools on the market, the free ones basically cannot be searched in batches. I see that there are only 10 queries at most on the Internet, and the query speed is very slow.

Fee-charging tools such as Ahrefs and SEMrush have a minimum monthly fee of 995/month. Of course, you can also buy them if you think the price is right. After all, many functions of these tools are very practical. The ranking search tool I will share with you today is based on python. Of course, it does not need to cost any money, just install the python development environment.

Implementation steps

Without further ado, let's go to the code

import requests
from bs4 import Beautifulsoup

First, we import two libraries, requests and Beautifulsoup, requests are 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  # 返回排名(从1开始)
        
        return -1  # 如果未找到网站,返回-1

    except requests.exceptions.RequestException as e:
        print(f"An error occurred: {
      
      e}")
        return None

The above code defines a function called get gogle_rank, which accepts two parameters: keyword (keyword) and ebsite (website domain name). The goal of the function is to get the ranking of the specified keywords in Google search results.

Inside the function, a URL is first constructed, which uses the specified keyword to perform public song boso. Then set a User-Agen header to simulate a browser request and use the requests.get method to send an HTTP request to obtain the response of the search result page. response.raise for_status() is used to check whether the request is successful. If the returned status code is not 200, an exception will be thrown.

Next, use the Beautifulsoup library to parse the HTML content of the response, create a Beautifulsoup object, and use the html, parser parser for parsing. Then use the find_a11 method to find all the div elements with the class attribute as g, and these elements contain the information of the search results.

Then use the enumerate function to traverse the list of search results, and use result.find(a"href1 to get the link in each search result. If the specified website domain name appears in the link, return the current ranking (counting from 1)

If the specified website domain name is not found after the loop ends, the function returns -1, indicating that the website was not found.

If an exception occurs during the request, it will get requests,exceptions.RequestException, print an error message, and return None.

# 示例用法
keywords = [摸鱼小游戏”,是男人就下100层",游戏
website = "haiyong.site'
for keyword in keywords:
6
rank = get google rank(keyword, website)
if rank is not None:
if rank == -1:
print(f“{
    
    keyword}没有排名")
10
11
else:
print(f"{
      
      keyword}排名第{
      
      rank}")

At the end is the code for an example usage. Defines a list keywords containing multiple keywords and a specified website domain name website

Through the for loop to traverse the keyword list, call the get_googe.rank function to get the ranking of each keyword in the Google search results. If the returned ranking is not one, make a conditional judgment based on the value of the ranking. If the ranking is -1, print a message that the keyword has no ranking, otherwise print the ranking information of the keyword.

The above is the meaning and logic of the entire code. This code realizes to obtain the ranking of the specified keywords in Google search results, and shows how to use this function through examples.

full 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  # 返回排名(从1开始)
        
        return -1  # 如果未找到网站,返回-1

    except requests.exceptions.RequestException as e:
        print(f"An error occurred: {
      
      e}")
        return None

# 示例用法
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}")

There is a problem with the ladder, let's put a screenshot of the query on Bing first.



Well, that's the end of today's sharing, see you next time.

Guess you like

Origin blog.csdn.net/ooowwq/article/details/131818898