Tập lệnh Python đơn giản kiểm tra vị trí của từ khóa trong kết quả của công cụ tìm kiếm bằng API JSON tìm kiếm tùy chỉnh của Google. Bạn sẽ cần đăng ký API và nhận khóa API. Ngoài ra, bạn cần tạo một công cụ tìm kiếm tùy chỉnh và lấy ID của nó.

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)

Đảm bảo thay thế "YOUR_API_KEY" bằng khóa Google API thực tế của bạn và "YOUR_CUSTOM_SEARCH_ENGINE_ID" bằng ID công cụ tìm kiếm tùy chỉnh của bạn.

Để sử dụng tập lệnh này, chỉ cần chạy nó trong môi trường Python của bạn và nó sẽ nhắc bạn nhập từ khóa bạn muốn kiểm tra vị trí, khóa API Google và ID Công cụ Tìm kiếm Tùy chỉnh của bạn. Sau đó nó sẽ hiển thị vị trí của từ khóa trong kết quả tìm kiếm nếu tìm thấy.

Bình luận (0)

Chưa có bình luận nào ở đây, bạn có thể là người đầu tiên!

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *


Chọn và mua proxy

Proxy trung tâm dữ liệu

Proxy luân phiên

Proxy UDP

Được tin cậy bởi hơn 10000 khách hàng trên toàn thế giới

Khách hàng ủy quyền
Khách hàng ủy quyền
Khách hàng proxy flowch.ai
Khách hàng ủy quyền
Khách hàng ủy quyền
Khách hàng ủy quyền