from urllib.request import urlopen
from bs4 import BeautifulSoup
import datetime
import random
import re
random.seed(datetime.datetime.now())
def getLinks(articleUrl):
html=urlopen("http://en.wikipedia.org"+articleUrl)#维基百科的域名加词条页面的URL链接
bsObj=BeautifulSoup(html)#解析HTML
#返回词条链接标签并抽取href属性
return bsObj.find("div",{"id":"bodyContent"}).findAll("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)
网络数据采集——遍历单个域名
最新推荐文章于 2025-07-08 23:26:43 发布