Google 맞춤 검색 JSON API를 사용하여 검색 엔진 결과에서 키워드 위치를 확인하는 간단한 Python 스크립트입니다. 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)
여기에는 아직 댓글이 없습니다. 첫 번째 댓글이 되실 수 있습니다!