forked from nittolese/gquestions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gquestions.py
351 lines (309 loc) · 11.4 KB
/
gquestions.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
usage='''
❓❔👾 Gquestions CLI Usage ❔❓
🔍 Usage:
gquestions.py query <keyword> (en|es) [depth <depth>] [--csv] [--headless]
gquestions.py (-h | --help)
💡 Examples:
▶️ gquestions.py query "flights" en Search "flights" in English and export in html
▶️ gquestions.py query "flights" en --headless Search headlessly "flights" in English and export in html
▶️ gquestions.py query "vuelos" es --csv Search "vuelos" in Spanish and export in html and csv
▶️ gquestions.py query "vuelos" es depth 1 Search "vuelos" in Spanish with a depth of 1 and export in html
▶️ gquestions.py -h Print this message
👀 Options:
-h, --help
'''
import os
import re
import sys
import json
import time
import datetime
import platform
from docopt import docopt
from tqdm import tqdm
from time import sleep
import pandas as pd
from pandas.io.json import json_normalize
import logging
from jinja2 import Environment, FileSystemLoader
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException, ElementClickInterceptedException
'''
Visualizza una barra di caricamento per mostrare l'attesa
'''
def sleepBar(seconds):
for i in tqdm(range(seconds)):
sleep(1)
def prettyOutputName(filetype='html'):
_query = re.sub('\s|\"|\/|\:|\.','_', query.rstrip())
prettyname = _query
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%d-%m-%Y_%H-%M-%S-%f')
if filetype != 'html':
prettyname += "_" + st + "." + filetype
else:
prettyname += "_" + st + "." + filetype
return prettyname
def initBrowser(headless=False):
if "Windows" in platform.system():
chrome_path = "driver/chromedriver.exe"
else:
chrome_path = "driver/chromedriver"
chrome_options = Options()
chrome_options.add_argument("--disable-features=NetworkService")
if headless:
chrome_options.add_argument('headless')
return webdriver.Chrome(options=chrome_options,executable_path=chrome_path)
"""
Search on Google and returns the list of PAA questions in SERP.
"""
def newSearch(browser,query):
if lang== "en":
browser.get("https://www.google.com?hl=en")
searchbox = browser.find_element_by_xpath("//input[@aria-label='Search']")
else:
browser.get("https://www.google.com?hl=es")
searchbox = browser.find_element_by_xpath("//input[@aria-label='Buscar']")
searchbox.send_keys(query)
sleepBar(2)
tabNTimes()
if lang== "en":
searchbtn = browser.find_elements_by_xpath("//input[@aria-label='Google Search']")
else:
searchbtn = browser.find_elements_by_xpath("//input[@aria-label='Buscar con Google']")
try:
searchbtn[-1].click()
except:
searchbtn[0].click()
sleepBar(2)
paa = browser.find_elements_by_xpath("//span/following-sibling::div[contains(@class,'match-mod-horizontal-padding')]")
hideGBar()
return paa
"""
Helper function that scroll into view the PAA questions element.
"""
def scrollToFeedback():
if lang == "en":
el = browser.find_element_by_xpath("//div[@class='kno-ftr']//div/following-sibling::a[text()='Feedback']")
else:
el = browser.find_element_by_xpath("//div[@class='kno-ftr']//div/following-sibling::a[text()='Enviar comentarios']")
actions = ActionChains(browser)
actions.move_to_element(el).perform()
browser.execute_script("arguments[0].scrollIntoView();", el)
actions.send_keys(Keys.PAGE_UP).perform()
sleepBar(1)
"""
Accessibility helper: press TAB N times (default 2)
"""
def tabNTimes(N=2):
actions = ActionChains(browser)
for _ in range(N):
actions = actions.send_keys(Keys.TAB)
actions.perform()
"""
Click on questions N times
"""
def clickNTimes(el, n=1):
for i in range(n):
el.click()
logging.info(f"clicking on ... {el.text}")
sleepBar(1)
scrollToFeedback()
try:
el.find_element_by_xpath("//*[@aria-expanded='true']").click()
except:
pass
sleepBar(1)
"""
Hide Google Bar to prevent ClickInterceptionError
"""
def hideGBar():
try:
browser.execute_script('document.getElementById("searchform").style.display = "none";')
except:
pass
"""
Where the magic happens
"""
def crawlQuestions(start_paa, paa_list, initialSet, depth=0):
_tmp = createNode(paa_lst=paa_list, name=query, children=True)
outer_cnt = 0
for q in start_paa:
scrollToFeedback()
if "Dictionary" in q.text:
continue
test = createNode(paa_lst=paa_list, n=0,
name=q.text,
parent=paa_list[0]["name"],
children=True)
clickNTimes(q)
new_q = showNewQuestions(initialSet)
for l, value in new_q.items():
sleepBar(1)
logging.info(f"{l}, {value.text}")
test1 = createNode(paa_lst=test[0]["children"][outer_cnt]["children"],
name=value.text,
parent=test[0]["children"][outer_cnt]["name"],
children=True)
initialSet = getCurrentSERP()
logging.info(f"Current count: {outer_cnt}")
outer_cnt += 1
if depth==1:
for i in range(depth):
currentQuestions = []
for i in initialSet.values():
currentQuestions.append(i.text)
for i in paa_list[0]["children"]:
for j in i["children"]:
parent = j['name']
logging.info(parent)
_tmpset = set()
if parent in currentQuestions:
try:
if "'" in parent:
xpath_compiler = '//div[text()="' + parent + '"]'
else:
xpath_compiler= "//div[text()='" + parent + "']"
question= browser.find_element_by_xpath(xpath_compiler)
except NoSuchElementException:
continue
scrollToFeedback()
sleepBar(1)
clickNTimes(question)
new_q = showNewQuestions(initialSet)
for l, value in new_q.items():
if value.text == parent:
continue
j['children'].append({"name": value.text,"parent": parent})
initialSet = getCurrentSERP()
"""
Get the current Result Page.
Returns:
A list with newest questions.
"""
def getCurrentSERP():
_tmpset = {}
new_paa = browser.find_elements_by_xpath("//span/following-sibling::div[contains(@class,'match-mod-horizontal-padding')]")
cnt= 0
for q in new_paa:
_tmpset.update({cnt:q})
cnt +=1
newInitialSet = _tmpset
return newInitialSet
"""
Shows new questions.
Args:
intialSet (dict): The initial set in the PAA box.
Returns:
list of questions with first 3-4 questions deleted (initalSet).
"""
def showNewQuestions(initialSet):
tmp = getCurrentSERP()
deletelist = [k for k, v in initialSet.items() if k in tmp and tmp[k] == v]
_tst = dict.copy(tmp)
for i,value in tmp.items():
if i in deletelist:
_tst.pop(i)
return _tst
"""
Create a new node in the list.
Args:
paa_list_elements: list of web elements
n: index of 'children' list on a main node
name: node nome
parent: Indicates if the node has a parent. Default to null only for first level.
chilren: Indicates if the node has a children list. default false
Returns:
list of questions with the current new node
"""
def createNode( n=-1, parent='null', children=False, name='',*, paa_lst):
logging.info(paa_lst)
if children:
_d = {
"name": name,
"parent": parent,
"children": []
}
else:
_d = {
"name": name,
"parent": parent
}
if n!=-1:
logging.info(paa_lst[n]["children"])
paa_lst[n]["children"].append(_d)
else:
paa_lst.append(_d)
return paa_lst
"""
This func takes in input JSON data and returns csv file.
"""
def flatten_csv(data,depth,prettyname):
try:
if depth == 0:
_ = json_normalize(data[0]["children"], 'children', ['name', 'parent',['children',]], record_prefix='inner.')
_.drop(columns=['children','inner.children','inner.parent'], inplace=True)
columnTitle = ['parent','name','inner.name']
_ = _.reindex(columns=columnTitle)
_.to_csv(prettyname,sep=";",encoding='utf-8')
elif depth == 1:
df = json_normalize(data[0]["children"], meta=['name','children','parent'], record_path="children", record_prefix='inner.')
frames = [ json_normalize(i) for i in df['inner.children'] ]
result = pd.concat(frames)
result.rename(columns={"name": "inner.inner.name", "parent": "inner.name"}, inplace=True)
merge = pd.merge(df, result, how='left', on="inner.name")
merge.drop(columns=['name'], inplace=True)
columnTitle = ['parent','inner.parent','inner.name','inner.inner.name']
merge = merge.reindex(columns=columnTitle)
merge = merge.drop_duplicates(subset='inner.inner.name', keep='first')
merge.to_csv(prettyname,sep=';',encoding='utf-8')
except Exception as e:
logging.warning(f"{e}")
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
args = docopt(usage)
print(args)
MAX_DEPTH = 1
if args['<depth>']:
depth = int(args['<depth>'])
if depth > MAX_DEPTH:
sys.exit("depth not allowed")
else:
depth = 0
if args['en']:
lang = "en"
elif args['es']:
lang = "es"
if args['<keyword>']:
if args['--headless']:
browser = initBrowser(True)
else:
browser = initBrowser()
query = args['<keyword>']
start_paa = newSearch(browser,query)
initialSet = {}
cnt= 0
for q in start_paa:
initialSet.update({cnt:q})
cnt +=1
paa_list = []
crawlQuestions(start_paa, paa_list, initialSet,depth)
treeData = 'var treeData = ' + json.dumps(paa_list) + ';'
if paa_list[0]['children']:
root = os.path.dirname(os.path.abspath(__file__))
templates_dir = os.path.join(root, 'templates')
env = Environment( loader = FileSystemLoader(templates_dir) )
template = env.get_template('index.html')
filename = os.path.join(root, 'html', prettyOutputName())
with open(filename, 'w') as fh:
fh.write(template.render(
treeData = treeData,
))
if args['--csv']:
if paa_list[0]['children']:
_path = 'csv/'+prettyOutputName('csv')
flatten_csv(paa_list, depth, _path)
browser.close()