-
Notifications
You must be signed in to change notification settings - Fork 257
/
gsearch.py
executable file
·247 lines (205 loc) · 7.01 KB
/
gsearch.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Create by Meibenjin.
#
# Last updated: 2018-12-15
#
# google search results crawler
import sys
import os
import urllib2
import socket
import time
import gzip
import StringIO
import re
import random
import types
from dotenv import load_dotenv, find_dotenv
from bs4 import BeautifulSoup
reload(sys)
sys.setdefaultencoding('utf-8')
# Load config from .env file
# TODO: Error handling
try:
load_dotenv(find_dotenv(usecwd=True))
base_url = os.environ.get('BASE_URL')
results_per_page = int(os.environ.get('RESULTS_PER_PAGE'))
except:
print "ERROR: Make sure you have .env file with proper config"
sys.exit(1)
user_agents = list()
# results from the search engine
# basically include url, title,content
class SearchResult:
def __init__(self):
self.url = ''
self.title = ''
self.content = ''
def getURL(self):
return self.url
def setURL(self, url):
self.url = url
def getTitle(self):
return self.title
def setTitle(self, title):
self.title = title
def getContent(self):
return self.content
def setContent(self, content):
self.content = content
def printIt(self, prefix=''):
print ('url\t->', self.url, '\n',
'title\t->', self.title, '\n',
'content\t->', self.content)
def writeFile(self, filename):
file = open(filename, 'a')
try:
file.write('url:' + self.url + '\n')
file.write('title:' + self.title + '\n')
file.write('content:' + self.content + '\n\n')
except IOError, e:
print ('file error:', e)
finally:
file.close()
class GoogleAPI:
def __init__(self):
timeout = 40
socket.setdefaulttimeout(timeout)
def randomSleep(self):
sleeptime = random.randint(60, 120)
time.sleep(sleeptime)
def extractDomain(self, url):
"""Return string
extract the domain of a url
"""
domain = ''
pattern = re.compile(r'http[s]?://([^/]+)/', re.U | re.M)
url_match = pattern.search(url)
if(url_match and url_match.lastindex > 0):
domain = url_match.group(1)
return domain
def extractUrl(self, href):
""" Return a string
extract a url from a link
"""
url = ''
pattern = re.compile(r'(http[s]?://[^&]+)&', re.U | re.M)
url_match = pattern.search(href)
if(url_match and url_match.lastindex > 0):
url = url_match.group(1)
return url
def extractSearchResults(self, html):
"""Return a list
extract serach results list from downloaded html file
"""
results = list()
soup = BeautifulSoup(html, 'html.parser')
div = soup.find('div', id='main')
if (type(div) == types.NoneType):
div = soup.find('div', id='center_col')
if (type(div) == types.NoneType):
div = soup.find('body')
if (type(div) != types.NoneType):
lis = div.findAll('a')
if(len(lis) > 0):
for link in lis:
if (type(link) == types.NoneType):
continue
url = link['href']
if url.find(".google") > 6:
continue
url = self.extractUrl(url)
if(cmp(url, '') == 0):
continue
title = link.renderContents()
title = re.sub(r'<.+?>', '', title)
result = SearchResult()
result.setURL(url)
result.setTitle(title)
span = link.find('div')
if (type(span) != types.NoneType):
content = span.renderContents()
content = re.sub(r'<.+?>', '', content)
result.setContent(content)
results.append(result)
return results
def search(self, query, lang='en', num=results_per_page):
"""Return a list of lists
search web
@param query -> query key words
@param lang -> language of search results
@param num -> number of search results to return
"""
search_results = list()
query = urllib2.quote(query)
if(num % results_per_page == 0):
pages = num / results_per_page
else:
pages = num / results_per_page + 1
for p in range(0, pages):
start = p * results_per_page
url = '%s/search?hl=%s&num=%d&start=%s&q=%s' % (
base_url, lang, results_per_page, start, query)
retry = 3
while(retry > 0):
try:
request = urllib2.Request(url)
length = len(user_agents)
index = random.randint(0, length-1)
user_agent = user_agents[index]
request.add_header('User-agent', user_agent)
request.add_header('connection', 'keep-alive')
request.add_header('Accept-Encoding', 'gzip')
request.add_header('referer', base_url)
response = urllib2.urlopen(request)
html = response.read()
if(response.headers.get('content-encoding', None) == 'gzip'):
html = gzip.GzipFile(
fileobj=StringIO.StringIO(html)).read()
results = self.extractSearchResults(html)
search_results.extend(results)
break
except urllib2.URLError, e:
print ('url error:', e)
self.randomSleep()
retry = retry - 1
continue
except Exception, e:
print ('error:', e)
retry = retry - 1
self.randomSleep()
continue
return search_results
def load_user_agent():
fp = open('./user_agents', 'r')
line = fp.readline().strip('\n')
while(line):
user_agents.append(line)
line = fp.readline().strip('\n')
fp.close()
def crawler():
# Load use agent string from file
load_user_agent()
# Create a GoogleAPI instance
api = GoogleAPI()
# set expect search results to be crawled
expect_num = 10
# if no parameters, read query keywords from file
if(len(sys.argv) < 2):
keywords = open('./keywords', 'r')
keyword = keywords.readline()
while(keyword):
results = api.search(keyword, num=expect_num)
for r in results:
r.printIt()
keyword = keywords.readline()
keywords.close()
else:
keyword = sys.argv[1]
results = api.search(keyword, num=expect_num)
for r in results:
r.printIt()
if __name__ == '__main__':
crawler()