Prosty skrypt w języku Python sprawdzający pozycję słowa kluczowego w wynikach wyszukiwania za pomocą interfejsu Google Custom Search JSON API. Musisz zarejestrować się w interfejsie API i uzyskać klucz API. Dodatkowo musisz utworzyć własną wyszukiwarkę i uzyskać jej identyfikator.
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)
Pamiętaj o wymianie "YOUR_API_KEY"
za pomocą aktualnego klucza Google API i "YOUR_CUSTOM_SEARCH_ENGINE_ID"
z identyfikatorem niestandardowej wyszukiwarki.
Aby użyć tego skryptu, po prostu uruchom go w środowisku Python, a wyświetli się monit o wprowadzenie słowa kluczowego, dla którego chcesz sprawdzić pozycję, klucza Google API i identyfikatora Twojej wyszukiwarki niestandardowej. Następnie wyświetli pozycję słowa kluczowego w wynikach wyszukiwania, jeśli zostanie znalezione.
Komentarze (0)
Nie ma tu jeszcze żadnych komentarzy, możesz być pierwszy!