forked from meibenjin/GoogleSearchCrawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsearch.py
executable file
·178 lines (150 loc) · 5.53 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
#!/usr/bin/python
#-*- coding: utf-8 -*-
#
# Create by Meibenjin.
#
# Last updated: 2013-04-02
#
# google search results crawler
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import urllib2, socket, time
import re, random, types
from bs4 import BeautifulSoup
base_url = 'https://www.google.com.hk/'
user_agents = ['Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20130406 Firefox/23.0', \
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0', \
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533+ \
(KHTML, like Gecko) Element Browser 5.0', \
'IBM WebExplorer /v0.94', 'Galaxy/1.0 [en] (Mac OS X 10.5.6; U; en)', \
'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)', \
'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14', \
'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) \
Version/6.0 Mobile/10A5355d Safari/8536.25', \
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) \
Chrome/28.0.1468.0 Safari/537.36', \
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0; TheWorld)']
# 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
print 'title\t->', self.title
print 'content\t->', self.content
print
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)
#extract the domain of a url
def extractDomain(self, 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
# extract serach results list from downloaded html file
def extractSearchResults(self, html):
results = list()
soup = BeautifulSoup(html)
div = soup.find('div', id = 'search')
if (type(div) != types.NoneType):
lis = div.findAll('li', {'class': 'g'})
if(len(lis) > 0):
for li in lis:
result = SearchResult()
h3 = li.find('h3', {'class': 'r'})
if(type(h3) == types.NoneType):
continue
# extract domain and title from h3 object
link = h3.find('a')
if (type(link) == types.NoneType):
continue
url = link['href']
url = self.extractDomain(url)
if(cmp(url, '') == 0):
continue
title = link.renderContents()
result.setURL(url)
result.setTitle(title)
span = li.find('span', {'class': 'st'})
if (type(span) != types.NoneType):
content = span.renderContents()
result.setContent(content)
results.append(result)
return results
# search web
# @param query -> query key words
# @param lang -> language of search results
# @param num -> number of search results to return
def search(self, query, lang='en', num=10):
query = urllib2.quote(query)
search_results = list()
url = '%s/search?hl=%s&num=%d&q=%s' % (base_url, lang, num, query)
str = ''
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')
response = urllib2.urlopen(request)
html = response.read()
results = self.extractSearchResults(html)
return results
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 test():
if(len(sys.argv) < 2):
print 'please enter search query.'
return
query = sys.argv[1]
api = GoogleAPI()
result = api.search(query)
for r in result:
r.printIt()
if __name__ == '__main__':
test()