Repository navigation
Expand file tree
/
Copy pathgsearch.py
More file actions
executable file
·174 lines (138 loc) · 4.41 KB
/
Copy pathgsearch.py
File metadata and controls
executable file
·174 lines (138 loc) · 4.41 KB
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
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
#
# Modified by JSonWulff
# Based of Meibenjin's orginal code:
# https://github.com/meibenjin/GoogleSearchCrawler
#
# Last updated: 2020-03-31
#
# Google adwords results crawler
import requests
import urllib
import random
import gzip
import io
import time
import re
import xlsxwriter
from bs4 import BeautifulSoup
from requests.exceptions import HTTPError
base_url = 'https://www.google.com'
user_agents = list()
class SearchResult:
def __init__(self):
self.url = ''
def getURL(self):
return self.url
def setURL(self, url):
self.url = url
def printIt(self, prefix=''):
print('url\t->', self.url, '\n')
def writeFile(self, filename):
file = open(filename, 'a')
try:
file.write('url:' + self.url + '\n')
except IOError as e:
print('file error:', e)
finally:
file.close()
class GoogleAPI:
def __init__(self):
self.searches = 0
def randomSleep(self):
sleeptime = random.randint(60, 120)
time.sleep(sleeptime)
def extractSearchResults(self, html):
results = list()
soup = BeautifulSoup(html, 'html.parser')
div = soup.find('div', id='main')
if (type(div) == type(None)):
div = soup.find('div', id='center_col')
if (type(div) == type(None)):
div = soup.find('body')
if (type(div) != type(None)):
ads = div.find_all('span', string="Annonce")
if (len(ads) > 0):
for ad in ads:
if (type(ads) == type(None)):
continue
adContainer2 = ad.parent
domain = re.search(
r'([a-zA-Z0-9-_]+\.*[a-zA-Z0-9][a-zA-Z0-9-_]+\.[a-zA-Z]{1,11}?.\/)', str(adContainer2))
results.append(domain.group())
return results
def search(self, query, lang='da'):
search_results = list()
query = urllib.parse.quote(query)
url = '%s/search?hl=%s&num=%d&start=%s&q=%s' % (
base_url, lang, 10, 0, query)
retry = 3
while (retry > 0):
try:
length = len(user_agents)
index = random.randint(0, length - 1)
user_agent = user_agents[index]
headers = {
'user-agent': user_agent,
'connection': 'keep-alive',
'Accept-Encoding': 'gzip',
'referer': base_url
}
response = requests.get(url, headers=headers)
html = response.text
results = self.extractSearchResults(html)
search_results.extend(results)
break
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
self.randomSleep()
retry = retry - 1
continue
except Exception as err:
print(f'Other error occurred: {err}')
self.randomSleep()
retry = retry - 1
continue
return search_results
def load_user_agent():
fp = open('./helperFiles/user_agents.txt', 'r')
line = fp.readline().strip('\n')
while (line):
user_agents.append(line)
line = fp.readline().strip('\n')
fp.close()
def load_from_file(path):
fp = open(path, 'r')
lines = []
line = fp.readline().strip('\n')
while (line):
lines.append(line)
line = fp.readline().strip('\n')
fp.close()
return lines
def crawler():
load_user_agent()
api = GoogleAPI()
soegeOrd = load_from_file('./searchWords.txt')
soegeBy = load_from_file('./byer.txt')
results = []
workbook = xlsxwriter.Workbook('results.xlsx')
worksheet = workbook.add_worksheet()
row = 0
for word in soegeOrd:
for by in soegeBy:
col = 0
query = (word + ' ' + by)
print(">> Finding search results for:", query)
worksheet.write(row, col, query)
newResult = api.search(query)
results.extend(newResult)
for result in newResult:
col += 1
worksheet.write(row, col, result)
row += 1
workbook.close()
print(">> Search result are ready in: results.xlsx")
if __name__ == '__main__':
crawler()