-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmultisearch.py
186 lines (144 loc) · 5.77 KB
/
multisearch.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
# coding=utf-8
__author__ = 'DM_'
import requests
import sys
import re
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4'
}
class search(object):
def __init__(self, word, pages, searchdict):
self.searchurl = searchdict['searchurl']
self.urlPattern = searchdict['urlPattern']
self.contentPattern = searchdict['contentPattern']
self.nextPattern = searchdict['nextPattern']
self.timeout = searchdict['timeout']
try:
self.proxies = searchdict['proxies']
except:
self.proxies = None
self.maxPages = searchdict['maxPages']
self.headers = headers
self.word = word
self.pages = pages
self.urls = []
self.contents = []
self.req = requests.session()
self.login()
def searchWithPages(self):
for page in xrange(1, self.pages + 1):
try:
searchurl = self.searchurl(self.word, page)
resp = self.req.get(searchurl, timeout=self.timeout, headers=headers)
percent = int((1.0 * page / self.pages) * 100)
sys.stdout.write("[+]Now is loading page %d, complete percent:%s. \r" % (page, str(percent) + '%'))
sys.stdout.flush()
html = resp.content
self.urls.extend(re.findall(self.urlPattern, html))
self.contents.extend(re.findall(self.contentPattern, html))
except Exception, e:
print "[!]%s \r" % e,
break
print
def searchall(self):
isNext = True
searchpage = 0
while isNext is not None:
try:
searchurl = self.searchurl(self.word, searchpage)
resp = self.req.get(searchurl, timeout=self.timeout, headers=headers)
html = resp.content
self.urls.extend(re.findall(self.urlPattern, html))
self.contents.extend(re.findall(self.contentPattern, html))
sys.stdout.write("[+]now is loading page %d ..\r" % (searchpage + 1))
sys.stdout.flush()
isNext = re.search(self.nextPattern, html)
searchpage += 1
except requests.HTTPError, e:
print u"[!]%s \r" % e,
break
print
print '[+]searched %d pages.\n' % searchpage
def search(self):
if self.maxPages:
if not self.pages is None and self.pages > self.maxPages:
self.pages = self.maxPages
else:
if self.nextPattern is None:
sys.exit("[!]error, not nextPattern so give a page for search.")
if self.pages is None:
self.searchall()
else:
self.searchWithPages()
self.urls = list(set(self.urls))
self.handle()
def login(self):
pass
def handle(self):
pass
bingsearchDict = {
'searchurl': lambda word, page: 'http://cn.bing.com/search?q=' + word + '&first=' + str(page - 1) + '1&FORM=PERE',
'urlPattern': r'<li class="b_algo"><h2><a href="([\s\S]+?)"[\s\S]+?</li>',
'contentPattern': '(?:<li class="b_algo">)(?:.+?)<p>([\s\S]+?)</p>(?:.+?)</li>',
'nextPattern': r'下一页',
"maxPages": None,
'timeout': 5,
}
googlesearchDict = {
'searchurl': lambda word, page: 'http://208.117.227.16/search?q=' + word + '&start=' + str(page) + '00&num=100',
'urlPattern': r'<li class="g">[\s\S]+?<a href="([\s\S]+?)"[\s\S]+?</li>',
'contentPattern': r'(?:<li class="g">)(?:.+?)<span class="st">(?:<span class="f">[\s\S]+?</span>)?([\s\S]+?)</span>(?:.+?)</li>',
'nextPattern': r'下一页',
"maxPages": None,
'timeout': 5,
}
so360searchDict = {
'searchurl': lambda word, page: 'http://www.so.com/s?ie=utf-8&q=' + word + '&pn=' + str(page),
'urlPattern': r'<h3 class="res-title ">[\s]+?<a href="([\s\S]+?)"',
'contentPattern': r'<p class="res-desc">([\s\S]+?)</p>',
'nextPattern': r'id="snext"',
"maxPages": None,
'timeout': 5,
}
sogousearchDict = {
'searchurl': lambda word, page: 'http://www.sogou.com/web?query=' + word + '&page=' + str(page) + '&num=100',
'urlPattern': r'id="uigs_d0_\d{1,2}" href="([\s\S]+?)"',
'contentPattern': r'<div class="ft" id="cacheresult_summary_\d+"> <!--summary_beg-->([\s\S]+?)<!--summary_end--></div>',
'nextPattern': 'sogou_next',
"maxPages": None,
'timeout': 5,
}
zoomeyesearchDict = {
'searchurl': lambda word, page: 'http://www.zoomeye.org/search?q=' + word + '&p=' + str(page),
'urlPattern': r'<h4><a href="(?:search\?q=)?([\s\S]+?)"',
'contentPattern': r' <div class="span7">([\s\S]+?)</div>',
'nextPattern': None,
"maxPages": 10,
'timeout': 5,
}
class simplesearch(search):
def __init__(self, word, pages, searchdict):
super(simplesearch, self).__init__(word, pages, searchdict)
class zoomeyesearch(search):
def __init__(self, word, pages, searchdict):
super(zoomeyesearch, self).__init__(word, pages, searchdict)
def login(self):
csrfcodeurl = "http://www.zoomeye.org/"
loginurl = "http://www.zoomeye.org/login"
resp = self.req.get(csrfcodeurl, headers=headers)
html = resp.content
csrfcode = re.findall("name='csrfmiddlewaretoken' value='([\s\S]+?)'", html)[0]
postdata = {
"csrfmiddlewaretoken" : csrfcode,
"username" : "xxxx",
"password":"xxxx",
}
self.req.post(loginurl, data=postdata, headers=headers)
if __name__ == "__main__":
s = simplesearch("site:x0day.me", None , googlesearchDict)
s.search()
print s.urls
z = zoomeyesearch("wordpress", None , zoomeyesearchDict)
z.search()
print z.urls
print len(z.urls)