-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path28未处理.py
More file actions
48 lines (40 loc) · 1.4 KB
/
Copy path28未处理.py
File metadata and controls
48 lines (40 loc) · 1.4 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
#维基百科六度分割篇:终结篇
from urllib.request import urlopen
from bs4 import BeautifulSoup
import pymysql
#连接数据库
conn = pymysql.connect(host='127.0.0.1',user='root',password=None,db='mysql',charset='utf8')
#设置游标,处理对数据库的输入和输出
cur = conn.cursor()
cur.execute("use wikipedia")
#class SolutionFound继承RuntimeError
class SolutionFound(RuntimeError):
def __init__(self,message):
self.message= message
def contructDict(currentPageId):
links = getLinks(currentPageId)
if links:
return dict(zip(links,[{}]*len(links)))
return {}
def searchDepth(targetPageId,currentPageId,linkTree,depth):
if depth==0:
return linkTree
if not linkTree:
linkTree = contructDict(currentPageId)
if not linkTree:
return {}
if targetPageId in linkTree.keys:
print("TARGET"+str(targetPageId)+" FOUND!")
raise SolutionFound("PAGE: "+str(currentPageId))
for branchKey,branchValue in linkTree.items():
try:
linkTree[branchKey] = searchDepth(targetPageId,branchKey,branchValue,depth-1)
except SolutionFound as e:
print(e.message)
raise SolutionFound("PAGE: "+str(currentPageId))
return linkTree
try:
searchDepth(134951,1,{},4)
print("No solution fonud")
except SolutionFound as e:
print(e.message)