Python: Script ການສະກັດເອົາແນວໂນ້ມຂອງ Google Autosuggest ສຳ ລັບ ຄຳ ຄົ້ນຫາ Niche ຂອງທ່ານ
ທຸກໆຄົນມັກ Google Trends, ແຕ່ມັນມີຄວາມຫຍຸ້ງຍາກຫລາຍເມື່ອເວົ້າເຖິງ Long Tail Keywords. ພວກເຮົາທຸກຄົນມັກທາງການ ບໍລິການແນວໂນ້ມຂອງ google ສຳ ລັບຄວາມເຂົ້າໃຈກ່ຽວກັບພຶດຕິ ກຳ ການຄົ້ນຫາ. ເຖິງຢ່າງໃດກໍ່ຕາມ, ສອງຢ່າງກີດຂວາງບໍ່ໃຫ້ຫຼາຍຄົນໃຊ້ມັນເພື່ອວຽກ ໜັກ;
- ເມື່ອທ່ານຕ້ອງການຊອກຫາ ຄຳ ສຳ ຄັນ ໃໝ່ໆ, ມີ ບໍ່ມີຂໍ້ມູນພຽງພໍໃນ Google Trends
- ຂາດ API ຢ່າງເປັນທາງການ ສຳ ລັບການຮ້ອງຂໍກ່ຽວກັບແນວໂນ້ມຂອງ google: ເມື່ອພວກເຮົາ ນຳ ໃຊ້ໂມດູນເຊັ່ນ pytrends, ຫຼັງຈາກນັ້ນພວກເຮົາຕ້ອງໃຊ້ເຄື່ອງແມ່ຂ່າຍຕົວແທນ, ຫຼືພວກເຮົາຖືກບລັອກ.
ໃນບົດຂຽນນີ້, ຂ້ອຍຈະແບ່ງປັນ Python Script ທີ່ພວກເຮົາຂຽນເພື່ອສົ່ງ ຄຳ ຫລັກທີ່ມີແນວໂນ້ມຜ່ານ Google Autosuggest.
ດຶງຂໍ້ມູນແລະເກັບຮັກສາຜົນໄດ້ຮັບຂອງ Autosuggest ໃນໄລຍະເວລາ
ສົມມຸດວ່າພວກເຮົາມີ 1,000 ແກ່ນ ຄຳ ທີ່ຖືກສົ່ງໄປຫາ Google Autosuggest. ໃນການຕອບແທນ, ພວກເຮົາອາດຈະໄດ້ຮັບປະມານ 200,000 ຫາງຍາວ ຄໍາສໍາຄັນ. ຈາກນັ້ນ, ພວກເຮົາຕ້ອງເຮັດແບບດຽວກັນໃນອາທິດຕໍ່ມາແລະປຽບທຽບຂໍ້ມູນເຫຼົ່ານີ້ເພື່ອຕອບສອງ ຄຳ ຖາມ:
- ຄຳ ຖາມໃດແມ່ນ ຄໍາຫລັກໃຫມ່ ເມື່ອທຽບກັບເວລາສຸດທ້າຍ? ນີ້ແມ່ນກໍລະນີທີ່ພວກເຮົາຕ້ອງການ. Google ຄິດວ່າການສອບຖາມເຫຼົ່ານັ້ນມີຄວາມ ສຳ ຄັນຫລາຍຂື້ນ - ໂດຍການເຮັດດັ່ງນັ້ນ, ພວກເຮົາສາມາດສ້າງວິທີແກ້ໄຂ Google Autosuggest ຂອງພວກເຮົາເອງ!
- ຄຳ ຖາມໃດແມ່ນ ຄໍາສໍາຄັນບໍ່ມີຕໍ່ໄປອີກແລ້ວ ແນວໂນ້ມ?
ບົດຂຽນແມ່ນຂ້ອນຂ້າງງ່າຍ, ແລະລະຫັດສ່ວນໃຫຍ່ທີ່ຂ້ອຍໄດ້ແບ່ງປັນ ທີ່ນີ້. ລະຫັດທີ່ຖືກປັບປຸງຈະຊ່ວຍປະຢັດຂໍ້ມູນຈາກການແລ່ນທີ່ຜ່ານມາແລະການປຽບທຽບ ຄຳ ແນະ ນຳ ໃນແຕ່ລະໄລຍະ. ພວກເຮົາຫລີກລ້ຽງຖານຂໍ້ມູນທີ່ອີງໃສ່ເອກະສານເຊັ່ນ SQLite ເພື່ອເຮັດໃຫ້ມັນງ່າຍດາຍ - ດັ່ງນັ້ນການເກັບຂໍ້ມູນທັງ ໝົດ ແມ່ນໃຊ້ໄຟລ໌ CSV ຢູ່ດ້ານລຸ່ມ. ນີ້ຊ່ວຍໃຫ້ທ່ານສາມາດ ນຳ ເຂົ້າເອກະສານໃນ Excel ແລະຄົ້ນຫາແນວໂນ້ມ ຄຳ ສຳ ຄັນຂອງທຸລະກິດຂອງທ່ານ.
ເພື່ອໃຊ້ປະໂຫຍດຈາກ Python Script ນີ້
- ໃສ່ຊຸດຄໍາຫລັກຂອງທ່ານທີ່ຄວນຈະຖືກສົ່ງໄປທີ່ autocomplete: keywords.csv
- ປັບການຕັ້ງຄ່າ Script ສຳ ລັບຄວາມຕ້ອງການຂອງທ່ານ:
- ພາສາ: ໃນຕອນຕົ້ນ“ en”
- ປະເທດ: ໃນຕອນຕົ້ນ“ ພວກເຮົາ”
- ຈັດຕາຕະລາງການຂຽນບົດເພື່ອເຮັດວຽກ ໜຶ່ງ ຄັ້ງຕໍ່ອາທິດ. ທ່ານຍັງສາມາດໃຊ້ມັນດ້ວຍຕົນເອງຕາມທີ່ທ່ານຕ້ອງການ.
- ໃຊ້ keyword_suggestions.csv ເພື່ອການວິເຄາະຕໍ່ໄປ:
- first_ເຫັນ: ນີ້ແມ່ນວັນທີ່ການສອບຖາມປະກົດຂື້ນເປັນເທື່ອ ທຳ ອິດໃນ autosuggest
- Last_seen: ວັນທີທີ່ການສອບຖາມໄດ້ເຫັນໃນຄັ້ງສຸດທ້າຍ
- ແມ່ນແລ້ວ: ຖ້າ first_seen == last_seen ພວກເຮົາຕັ້ງຄ່ານີ້ ທີ່ແທ້ຈິງ - ພຽງແຕ່ກັ່ນຕອງໃສ່ຄຸນຄ່ານີ້ເພື່ອໃຫ້ໄດ້ການຄົ້ນຫາແບບ ໃໝ່ ໃນ Google autosuggest.
ນີ້ແມ່ນລະຫັດ Python
# Pemavor.com Autocomplete Trends
# Author: Stefan Neefischer (stefan.neefischer@gmail.com)
import concurrent.futures
from datetime import date
from datetime import datetime
import pandas as pd
import itertools
import requests
import string
import json
import time
charList = " " + string.ascii_lowercase + string.digits
def makeGoogleRequest(query):
# If you make requests too quickly, you may be blocked by google
time.sleep(WAIT_TIME)
URL="http://suggestqueries.google.com/complete/search"
PARAMS = {"client":"opera",
"hl":LANGUAGE,
"q":query,
"gl":COUNTRY}
response = requests.get(URL, params=PARAMS)
if response.status_code == 200:
try:
suggestedSearches = json.loads(response.content.decode('utf-8'))[1]
except:
suggestedSearches = json.loads(response.content.decode('latin-1'))[1]
return suggestedSearches
else:
return "ERR"
def getGoogleSuggests(keyword):
# err_count1 = 0
queryList = [keyword + " " + char for char in charList]
suggestions = []
for query in queryList:
suggestion = makeGoogleRequest(query)
if suggestion != 'ERR':
suggestions.append(suggestion)
# Remove empty suggestions
suggestions = set(itertools.chain(*suggestions))
if "" in suggestions:
suggestions.remove("")
return suggestions
def autocomplete(csv_fileName):
dateTimeObj = datetime.now().date()
#read your csv file that contain keywords that you want to send to google autocomplete
df = pd.read_csv(csv_fileName)
keywords = df.iloc[:,0].tolist()
resultList = []
with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
futuresGoogle = {executor.submit(getGoogleSuggests, keyword): keyword for keyword in keywords}
for future in concurrent.futures.as_completed(futuresGoogle):
key = futuresGoogle[future]
for suggestion in future.result():
resultList.append([key, suggestion])
# Convert the results to a dataframe
suggestion_new = pd.DataFrame(resultList, columns=['Keyword','Suggestion'])
del resultList
#if we have old results read them
try:
suggestion_df=pd.read_csv("keyword_suggestions.csv")
except:
suggestion_df=pd.DataFrame(columns=['first_seen','last_seen','Keyword','Suggestion'])
suggestionCommon_list=[]
suggestionNew_list=[]
for keyword in suggestion_new["Keyword"].unique():
new_df=suggestion_new[suggestion_new["Keyword"]==keyword]
old_df=suggestion_df[suggestion_df["Keyword"]==keyword]
newSuggestion=set(new_df["Suggestion"].to_list())
oldSuggestion=set(old_df["Suggestion"].to_list())
commonSuggestion=list(newSuggestion & oldSuggestion)
new_Suggestion=list(newSuggestion - oldSuggestion)
for suggest in commonSuggestion:
suggestionCommon_list.append([dateTimeObj,keyword,suggest])
for suggest in new_Suggestion:
suggestionNew_list.append([dateTimeObj,dateTimeObj,keyword,suggest])
#new keywords
newSuggestion_df = pd.DataFrame(suggestionNew_list, columns=['first_seen','last_seen','Keyword','Suggestion'])
#shared keywords with date update
commonSuggestion_df = pd.DataFrame(suggestionCommon_list, columns=['last_seen','Keyword','Suggestion'])
merge=pd.merge(suggestion_df, commonSuggestion_df, left_on=["Suggestion"], right_on=["Suggestion"], how='left')
merge = merge.rename(columns={'last_seen_y': 'last_seen',"Keyword_x":"Keyword"})
merge["last_seen"].fillna(merge["last_seen_x"], inplace=True)
del merge["last_seen_x"]
del merge["Keyword_y"]
#merge old results with new results
frames = [merge, newSuggestion_df]
keywords_df = pd.concat(frames, ignore_index=True, sort=False)
# Save dataframe as a CSV file
keywords_df['first_seen'] = pd.to_datetime(keywords_df['first_seen'])
keywords_df = keywords_df.sort_values(by=['first_seen','Keyword'], ascending=[False,False])
keywords_df['first_seen']= pd.to_datetime(keywords_df['first_seen'])
keywords_df['last_seen']= pd.to_datetime(keywords_df['last_seen'])
keywords_df['is_new'] = (keywords_df['first_seen']== keywords_df['last_seen'])
keywords_df=keywords_df[['first_seen','last_seen','Keyword','Suggestion','is_new']]
keywords_df.to_csv('keyword_suggestions.csv', index=False)
# If you use more than 50 seed keywords you should slow down your requests - otherwise google is blocking the script
# If you have thousands of seed keywords use e.g. WAIT_TIME = 1 and MAX_WORKERS = 5
WAIT_TIME = 0.2
MAX_WORKERS = 20
# set the autocomplete language
LANGUAGE = "en"
# set the autocomplete country code - DE, US, TR, GR, etc..
COUNTRY="US"
# Keyword_seed csv file name. One column csv file.
#csv_fileName="keyword_seeds.csv"
CSV_FILE_NAME="keywords.csv"
autocomplete(CSV_FILE_NAME)
#The result will save in keyword_suggestions.csv csv file