-
Notifications
You must be signed in to change notification settings - Fork 0
/
奇妙搜索.py
604 lines (441 loc) · 13.8 KB
/
奇妙搜索.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# -*- coding: utf-8 -*-
import urllib.request
import urllib.parse
import urllib.error
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import jieba
class Node(object): #节点结构体
def __init__(self, val,wh,p=0):
self.data = val #val为string类型
self.webhead = wh
self.next = p
class LinkList(object):#链表
def __init__(self):#初始化
self.head = 0
def __getitem__(self, key):#得到某一节点的内容,key相当于节点下标
if self.is_empty():
print ('linklist is empty.')
return
elif key < 0 or key > self.getlength():
print ('the given key is error')
return
else:
return self.getitem(key)
def __setitem__(self, key, value):#修改某节点内容
if self.is_empty():
print ('linklist is empty.')
return
elif key <0 or key > self.getlength():
print ('the given key is error')
return
else:
self.delete(key)
return self.insert(key)
def initlist(self,data,webhead):#初始化链表
self.head = Node(data,webhead)
def getlength(self):#返回链表长度
p = self.head
length = 0
while p!=0:
length+=1
p = p.next
return length
def is_empty(self):#判断链表是否为空
if self.getlength() ==0:
return True
else:
return False
def clear(self):#清空链表
self.head = 0
def append(self,data,wh):#队尾插入
q = Node(data,wh)
if self.head ==0:
self.head = q
else:
p = self.head
while p.next!=0:
p = p.next
p.next = q
def getdata(self,index):#得到任意节点内容
if self.is_empty():
print ('Linklist is empty.')
return
j = 0
p = self.head
while p.next!=0 and j <index:
p = p.next
j+=1
if j ==index:
return p.data
else:
print ('target is not exist!')
def getwh(self,index):#得到任意节点内容
if self.is_empty():
print ('Linklist is empty.')
return
j = 0
p = self.head
while p.next!=0 and j <index:
p = p.next
j+=1
if j ==index:
return p.webhead
else:
print ('target is not exist!')
def insert(self,index,data,wh):#在任意位置插入
if self.is_empty() or index<0 or index >self.getlength():
print ('Linklist is empty.')
return
if index ==0:
q = Node(data,wh,self.head)
self.head = q
p = self.head
post = self.head
j = 0
while p.next!=0 and j<index:
post = p
p = p.next
j+=1
if index ==j:
q = Node(data,wh,p)
post.next = q
q.next = p
def delete(self,index):#删除任意节点
if self.is_empty() or index<0 or index >self.getlength():
print ('Linklist is empty.')
return
if index == 0:
self.head = self.head.next
p = self.head
post = self.head
j = 0
while p.next!=0 and j<index:
post = p
p = p.next
j+=1
if index == j:
post.next = p.next
def index(self,value):#查找链表中内容等于value的内容
if self.is_empty():
print ('Linklist is empty.')
return
p = self.head
i = 0
while p.next!=0 and not p.data == value:
p = p.next
i+=1
if p.data == value:
return i
else:
return -1
class Web(object): #节点结构体
def __init__(self, val, p=0):
self.data = val #val为string类型
self.next = p
class WebList(object):#链表
def __init__(self):#初始化
self.head = 0
def __getitem__(self, key):#得到某一节点的内容,key相当于节点下标
if self.is_empty():
print ('linklist is empty.')
return
elif key <0 or key > self.getlength():
print ('the given key is error')
return
else:
return self.getitem(key)
def __setitem__(self, key, value):#修改某节点内容
if self.is_empty():
print ('linklist is empty.')
return
elif key <0 or key > self.getlength():
print ('the given key is error')
return
else:
self.delete(key)
return self.insert(key)
def initlist(self,data):#初始化链表
self.head = Web(data)
def getlength(self):#返回链表长度
p = self.head
length = 0
while p!=0:
length+=1
p = p.next
return length
def is_empty(self):#判断链表是否为空
if self.getlength() ==0:
return True
else:
return False
def clear(self):#清空链表
self.head = 0
def append(self,data):#队尾插入
q = Web(data)
if self.head ==0:
self.head = q
else:
p = self.head
while p.next!=0:
p = p.next
p.next = q
def getdata(self,index):#得到任意节点内容
if self.is_empty():
print ('Linklist is empty.')
return
j = 0
p = self.head
while p.next!=0 and j <index:
p = p.next
j+=1
if j ==index:
return p.data
else:
print ('target is not exist!')
def insert(self,index,data):#在任意位置插入
if self.is_empty() or index<0 or index >self.getlength():
print ('Linklist is empty.')
return
if index ==0:
q = Web(data, self.head)
self.head = q
p = self.head
post = self.head
j = 0
while p.next!=0 and j<index:
post = p
p = p.next
j+=1
if index ==j:
q = Web(data, p)
post.next = q
q.next = p
def delete(self,index):#删除任意节点
if self.is_empty() or index<0 or index >self.getlength():
print ('Linklist is empty.')
return
if index == 0:
self.head = self.head.next
p = self.head
post = self.head
j = 0
while p.next!=0 and j<index:
post = p
p = p.next
j+=1
if index == j:
post.next = p.next
def index(self,value):#查找链表中内容等于value的内容
if self.is_empty():
print ('Linklist is empty.')
return
p = self.head
i = 0
while p.next!=0 and not p.data == value:
p = p.next
i+=1
if p.data == value:
return i
else:
return -1
class Result(object): #节点结构体
def __init__(self, val, p=0):
self.data = val #val为string类型
self.next = p
class ResultList(object):#链表
def __init__(self):#初始化
self.head = 0
def __getitem__(self, key):#得到某一节点的内容,key相当于节点下标
if self.is_empty():
print ('linklist is empty.')
return
elif key <0 or key > self.getlength():
print ('the given key is error')
return
else:
return self.getitem(key)
def __setitem__(self, key, value):#修改某节点内容
if self.is_empty():
print ('linklist is empty.')
return
elif key <0 or key > self.getlength():
print ('the given key is error')
return
else:
self.delete(key)
return self.insert(key)
def initlist(self,data):#初始化链表
self.head = Result(data)
def getlength(self):#返回链表长度
p = self.head
length = 0
while p!=0:
length+=1
p = p.next
return length
def is_empty(self):#判断链表是否为空
if self.getlength() ==0:
return True
else:
return False
def clear(self):#清空链表
self.head = 0
def append(self,data):#队尾插入
q = Result(data)
if self.head ==0:
self.head = q
else:
p = self.head
while p.next!=0:
p = p.next
p.next = q
def getdata(self,index):#得到任意节点内容
if self.is_empty():
print ('Linklist is empty.')
return
j = 0
p = self.head
while p.next!=0 and j <index:
p = p.next
j+=1
if j ==index:
return p.data
else:
print ('target is not exist!')
def insert(self,index,data):#在任意位置插入
if self.is_empty() or index<0 or index >self.getlength():
print ('Linklist is empty.')
return
if index ==0:
q = Result(data, self.head)
self.head = q
p = self.head
post = self.head
j = 0
while p.next!=0 and j<index:
post = p
p = p.next
j+=1
if index ==j:
q = Result(data, p)
post.next = q
q.next = p
def delete(self,index):#删除任意节点
if self.is_empty() or index<0 or index >self.getlength():
print ('Linklist is empty.')
return
if index == 0:
self.head = self.head.next
p = self.head
post = self.head
j = 0
while p.next!=0 and j<index:
post = p
p = p.next
j+=1
if index == j:
post.next = p.next
def index(self,value):#查找链表中内容等于value的内容
if self.is_empty():
print ('Linklist is empty.')
return
p = self.head
i = 0
while p.next!=0 and not p.data == value:
p = p.next
i+=1
if p.data == value:
return i
else:
return -1
ignorewords=set(['的','但是','然而','能','在','以及','可以','使'])
#ignorewords=set(['the','of','to','and','a','in','is','it'])
ll = LinkList()
class crawler:
def __init__(self,dbname):
pass
def __del__(self):
pass
def addtoindex(self,url,soup):
#if self.isindexed(url): return
print('Indexing '+url)
text=self.gettextonly(soup) #获取文本信息
words=self.separatewords(text) #分词
dicn=list(words)
for i in range(len(dicn)):
word=dicn[i]
if word in ignorewords: continue
if ll.getlength() == 0:
wl = WebList()
wl.initlist(url)
ll.initlist(word,wl)
if ll.getlength() > 0:
i = ll.index(word)
if i == -1:
wl = WebList()
wl.initlist(url)
ll.append(word,wl)
#print(word)
if i != -1:
j = ll.getwh(i).index(url)
if j == -1:
ll.getwh(i).append(url)
#从一个Html网页中提取文字(不带标签的)
def gettextonly(self,soup):
v=soup.string
if v==None:
c=soup.contents
resulttext=''
for t in c: #获取文本信息
resulttext+=self.gettextonly(t)+'\n'
return resulttext
else:
return v.strip()
def separatewords(self,text): #结巴分词
seg_list=jieba.cut_for_search(text);
return seg_list
'''def separatewords(self,text):
##该方法仅仅局限于分英文单词
splitter=re.compile('\\W*')
return [s.lower() for s in splitter.split(text) if s!=' ']
'''
#从一组小网页开始进行广度优先搜索,直到某一给定深度,期间为网页建立索引
def crawl(self,pages,depth=2):
for i in range(depth):
newpages=set()
for page in pages: #广度
try:
c=urllib.request.urlopen(page)
except:
print ("Could not open %s" % page)
continue
soup=BeautifulSoup(c.read())
self.addtoindex(page,soup) #索引
links=soup('a')
for link in links:
if('href' in dict(link.attrs)):
url=urljoin(page,link['href'])
if url.find("'")!=-1:continue
url=url.split('#')[0] #去锚点
if url[0:4]=='http':
newpages.add(url)
pages=newpages
pagelist=['http://www.sdust.edu.cn/']
cra=crawler('')
cra.crawl(pagelist)
search = input("Please putin your keyword: ")
rl = ResultList()
rl.initlist('CONGCONG')
#seach进行结巴分词
seg_list = jieba.cut_for_search(search)
for sword in seg_list:
k = ll.index(sword)
if k != -1:
p = ll.getwh(k).getlength()
for m in range(0,p):
b = ll.getwh(k).getdata(m)
if rl.index(b) == -1:
rl.append(b)
print(b)
# python3 /Users/amazingtai/Desktop/Python/pachong/奇妙搜索.py