简单的 Python 脚本,使用 Google 自定义搜索 JSON API 检查关键字在搜索引擎结果中的位置。您需要注册 API 并获取 API 密钥。此外,您还需要创建自定义搜索引擎并获取其 ID。

import requests

def get_search_results(keyword, api_key, cx):
    url = f"https://www.googleapis.com/customsearch/v1?key={api_key}&cx={cx}&q={keyword}"
    response = requests.get(url)
    if response.status_code == 200:
        data = response.json()
        return data.get('items', [])
    else:
        print("Error occurred:", response.status_code)
        return []

def find_keyword_position(search_results, keyword):
    for index, result in enumerate(search_results, start=1):
        if keyword.lower() in result['title'].lower() or keyword.lower() in result['snippet'].lower():
            return index
    return -1

def check_keyword_position(keyword, api_key, cx):
    search_results = get_search_results(keyword, api_key, cx)
    if search_results:
        position = find_keyword_position(search_results, keyword)
        if position != -1:
            print(f"The keyword '{keyword}' is found at position {position} in search results.")
        else:
            print(f"The keyword '{keyword}' is not found in the first 10 search results.")
    else:
        print("No search results found.")

if __name__ == "__main__":
    keyword = input("Enter the keyword to check position for: ")
    api_key = input("Enter your Google API key: ")
    cx = input("Enter your Custom Search Engine ID: ")
    check_keyword_position(keyword, api_key, cx)

确保更换 "YOUR_API_KEY" 使用您的实际 Google API 密钥和 "YOUR_CUSTOM_SEARCH_ENGINE_ID" 使用您的自定义搜索引擎ID。

要使用此脚本,只需在您的 Python 环境中运行它,它会提示您输入要检查排名的关键字、您的 Google API 密钥和您的自定义搜索引擎 ID。然后,它会显示关键字在搜索结果中的位置(如果找到)。

评论 (0)

这里还没有评论,你可以成为第一个评论者!

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注


选择和购买代理

数据中心代理

轮流代理

UDP代理机构

受到全球 10000 多家客户的信赖

代理客户
代理客户
代理客户 flowch.ai
代理客户
代理客户
代理客户