-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-serp.py
162 lines (118 loc) · 5.23 KB
/
google-serp.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
import argparse
import urllib
import os
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import undetected_chromedriver as uc
import anticaptcha
import json
import csv
import sys
class Google:
def __init__(self, proxy=None, headless=False):
options = uc.ChromeOptions()
chrome_version = get_chrome_major_version()
if proxy is not None:
options.add_argument('--proxy-server={}'.format(proxy))
options.headless = headless
if headless:
sys.stderr.write('Warning: Headless mode enabled. It is experimental and not guaranteed to work.\n')
# Enable headless mode if there is no X server
elif os.environ.get('DISPLAY') is None:
options.headless = True
sys.stderr.write('Warning: DISPLAY environment variable is not set. Headless mode enabled. It is experimental and not guaranteed to work.\n')
self.driver = None
start_url = 'https://google.com/ncr'
if anticaptcha.key_available():
self.driver = anticaptcha.open_undetected_chrome(start_url, options=options, version_main=chrome_version)
else:
self.driver = uc.Chrome(options, version_main=chrome_version)
self.driver.get(start_url)
self.wait = WebDriverWait(self.driver, 30)
selector = 'span>div>div>div>div>div>button'
self.wait.until(lambda driver: driver.find_element(By.CSS_SELECTOR, selector))
buttons = self.driver.find_elements(By.CSS_SELECTOR, selector)
buttons[1].click()
def __del__(self):
if self.driver is not None:
self.driver.quit()
def parse_serp(self, query, max_page=100):
query = query.replace('_', ' ')
url = 'https://google.com/search?q={}'.format(urllib.parse.quote(query))
self.driver.get(url)
results = list()
next_page = 1
while True:
self.wait.until(lambda driver: driver.find_element(By.CSS_SELECTOR, 'a h3'))
position = 0
for h3 in self.driver.find_elements(By.CSS_SELECTOR, 'a h3'):
a = h3.find_element(By.XPATH, '..')
title = h3.text
url = a.get_attribute('href')
try:
snippet = h3.find_element(By.XPATH, '../../../..//span/em/..').text
except:
snippet = None
position = position + 1
result = {
'url': url,
'title': title,
'snippet': snippet,
'query': query,
'page': next_page,
'position': position,
}
results.append(result)
next_page = next_page + 1
if next_page > max_page:
return results
found = False
for a in self.driver.find_elements(By.CSS_SELECTOR, 'td a[aria-label]'):
label = a.get_attribute('aria-label')
if label == 'Page {}'.format(next_page):
a.click()
found = True
break
if not found:
return results
def get_chrome_major_version():
output = os.popen('chromium-browser --version').read().split(' ')
if output[0] == 'Chromium':
return output[1].split('.')[0]
return None
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Google SERP Parser')
parser.add_argument('-o', '--output', help='Output File', required=False)
parser.add_argument('-n', '--limit', help='Max Number of Pages', required=False, default='100')
parser.add_argument('-p', '--proxy', help='Proxy Address', required=False)
parser.add_argument('-e', '--headless', help='Headless Mode', action='store_true', required=False, default=False)
parser.add_argument('arg',
type=str,
nargs='*',
help='Queries to parse')
args = vars(parser.parse_args())
output_file = args['output']
page_limit = int(args['limit'])
if len(args['arg']) > 0:
goog = Google(args['proxy'], args['headless'])
for query in args['arg']:
try:
results = goog.parse_serp(query, page_limit)
if output_file is not None:
# ext is .json
if output_file[-5:] == '.json':
with open(output_file, 'a') as f:
f.write(json.dumps(results))
else:
with open(output_file, 'a', newline='') as f:
if output_file[-4:] == '.tsv':
delim = '\t'
else:
delim = ','
w = csv.writer(f, delimiter=delim, quotechar='"', quoting=csv.QUOTE_MINIMAL)
for link in results:
w.writerow([link['url'], link['title'], link['snippet'], link['query'], link['page'], link['position']])
except Exception as e:
print('{}: {}'.format(query, e))
else:
print('No query provided')