from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import random
import datetime
random.seed(datetime.datetime.now())
def getLinks(articleUrl):
html=urlopen("http://en.wikipedia.org" + articleUrl).read()
bsobj=BeautifulSoup(html,"html.parser")
return bsobj.find("div",{id:"bodyContent"}). find("a",{"href", re.compile("^(/wiki/)((?!:).)*$")})
links=getLinks("/wiki/kevin_Bacon")
while len(links)>0:
newArticle=links[random.randint(0,len(links)-1)].attrs["href"]
print(newArticle)
links=getLinks(newArticle)