Demo entry 6166552



Submitted by a on Oct 09, 2016 at 06:35
Language: Python 3. Code size: 1.7 kB.

from urllib.request import urlopen
from bs4 import BeautifulSoup
import pymysql

conn = pymysql.connect(host='',user='root', passwd=None, db='mysql', charset='utf8')
cur = conn.cursor()
cur.execute("USE wikipedia")

class SolutionFound(RuntimeError):
    def __init__(self, message):
        self.message = message

def getLinks(fromPageId):
    cur.execute("SELECT toPageId FROM links WHERE fromPageId = %s", (fromPageId))
    if cur.rowcount == 0:
        return None
        return [x[0] for x in cur.fetchall()]

def constructDict(currentPageId):
    links = getLinks(currentPageId)
    if links:
        return dict(zip(links, [{}]*len(links)))
    return {}

#The link tree may either be empty or contain multiple links
def searchDepth(targetPageId, currentPageId, linkTree, depth):
    if depth == 0:
#Stop recursing and return, regardless
        return linkTree
    if not linkTree:
        linkTree = constructDict(currentPageId)
    if not linkTree:
#No links found. Cannot continue at this node
        return {}
    if targetPageId in linkTree.keys():
        print("TARGET "+str(targetPageId)+" FOUND!")
        raise SolutionFound("PAGE: "+str(currentPageId))
    for branchKey, branchValue in linkTree.items():
#Recurse here to continue building the tree
            linkTree[branchKey] = searchDepth(targetPageId, branchKey,branchValue, depth-1)
        except SolutionFound as e:
            raise SolutionFound("PAGE: "+str(currentPageId))
    return linkTree

    searchDepth(134951, 1, {}, 4)
    print("No solution found")
except SolutionFound as e:

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).