from bs4 import BeautifulSoup
import requests
import re
import random
import datetime
random.seed(datetime.datetime.now())
def getLinks(articleUrl):
html = requests.get('http://en.wikipedia.org/wiki/Kevin_Bacon')
soup = BeautifulSoup(html.text, 'lxml')
return soup.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)
不明白这里 newArticle = links[random.randint(0, len(links)-1)].attrs['href']
links 为什么不能用(),一定要用[ ]
import requests
import re
import random
import datetime
random.seed(datetime.datetime.now())
def getLinks(articleUrl):
html = requests.get('http://en.wikipedia.org/wiki/Kevin_Bacon')
soup = BeautifulSoup(html.text, 'lxml')
return soup.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)
不明白这里 newArticle = links[random.randint(0, len(links)-1)].attrs['href']
links 为什么不能用(),一定要用[ ]