-
Notifications
You must be signed in to change notification settings - Fork 5
/
keyword-planner-api.py
131 lines (118 loc) · 4.15 KB
/
keyword-planner-api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import csv, time, re, math
from googleads import adwords
class googleKeywordPlanner:
'''
Python module to connect to the Google Keyword Planner API and get average search volume
and 12 month trends for seed keywords read from a CSV
'''
locationID = 2840
languageID = 1000
keywords = {}
dateRanges = []
def __init__(self, keywordList = [], getIdeas = False, listName = ''):
if keywordList != []:
chunkSize = 700
chunks = math.ceil(len(keywordList) / chunkSize)
keywordChunks = []
for thisChunk in range(0,chunks):
keywordChunks.append(keywordList[thisChunk * chunkSize:(thisChunk + 1) * chunkSize])
cCount = 0
for thisChunk in keywordChunks:
cCount = cCount + 1
print(cCount,'of',chunks,'- Getting data...')
self.getData(thisChunk,"STATS")
if getIdeas == True:
for thisKW in keywordList:
self.getData([thisKW],'IDEAS')
if listName != '':
self.listName = listName
'''
Point to your Google Account YAML file
'''
client = adwords.AdWordsClient.LoadFromStorage('googleads.yaml')
def getData(self,keywordList,requestType):
targeting_idea_service = self.client.GetService('TargetingIdeaService', version='v201809')
offset = 0
results = 1000
selector = {'ideaType': 'KEYWORD','requestType': requestType}
selector['requestedAttributeTypes'] = ['KEYWORD_TEXT', 'SEARCH_VOLUME', 'CATEGORY_PRODUCTS_AND_SERVICES', 'TARGETED_MONTHLY_SEARCHES', 'COMPETITION', 'AVERAGE_CPC']
selector['paging'] = {'startIndex': str(offset),'numberResults': str(results)}
selector['searchParameters'] = [
{
'xsi_type': 'RelatedToQuerySearchParameter',
'queries': keywordList
},
{
'xsi_type': 'LocationSearchParameter',
'locations': [{'id': self.locationID}]
},
{
'xsi_type': 'LanguageSearchParameter',
'languages': [{'id': self.languageID}]
}
]
ideas = None
'''
Back off requests if the API returns an error
Add 5 seconds each time we're throttled and give up after 5 attempts
'''
retries = 0
while retries < 4:
try:
ideas = targeting_idea_service.get(selector)
break
except Exception as e:
wait = (5 * retries) + 1
print('API error - waiting',wait,'seconds.',e)
time.sleep(wait)
retries = retries + 1
if ideas != None:
for thisIdea in ideas['entries']:
thisKeyword = {}
for thisResult in thisIdea['data']:
thisKeyword[thisResult['key']] = getattr(thisResult['value'], 'value', '0')
kwID = len(self.keywords)
self.keywords[kwID] = thisKeyword
if self.dateRanges == []:
for thisDate in thisKeyword['TARGETED_MONTHLY_SEARCHES']:
self.dateRanges.append(str(thisDate['month']) + "/" + str(thisDate['year']))
self.dateRanges.reverse()
return True
else:
return False
def exportCSV(self):
with open(self.listName + ' - volume.csv', 'w', encoding='utf-8', newline = '') as csvfile:
output = csv.writer(csvfile, delimiter=',', quotechar='"')
csvHeader = ['Keyword','AdWords Categories','Competition','Average CPC','Average Search Volume','YoY Change']
csvHeader += self.dateRanges
output.writerow(csvHeader)
for kwID,thisKeyword in self.keywords.items():
kwTrend = []
yoy = 0
if self.keywords[kwID]['TARGETED_MONTHLY_SEARCHES'] != None:
for thisMonth in self.keywords[kwID]['TARGETED_MONTHLY_SEARCHES']:
kwTrend.append(thisMonth['count'])
kwTrend.reverse()
if kwTrend[0] != 0 and kwTrend[11] != None:
yoy = (kwTrend[11] - kwTrend[0]) / kwTrend[0]
cpc = 0
if self.keywords[kwID]['AVERAGE_CPC'] != None:
cpc = self.keywords[kwID]['AVERAGE_CPC']['microAmount'] / 1000000
thisRow = [
self.keywords[kwID]['KEYWORD_TEXT'],
self.keywords[kwID]['CATEGORY_PRODUCTS_AND_SERVICES'],
self.keywords[kwID]['COMPETITION'],
cpc,
self.keywords[kwID]['SEARCH_VOLUME'],
yoy,
] + kwTrend
output.writerow(thisRow)
kwp = googleKeywordPlanner()
csvPath = 'keyword list.csv'
with open(csvPath,'r', encoding = 'utf-8') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
kwIdeas = []
for row in csv_reader:
kwIdeas.append(row[0])
kwp = googleKeywordPlanner(kwIdeas,False,'Keyword List Volume')
kwp.exportCSV()