from bs4 import BeautifulSoup
from urllib.request import urlopen
import re
import random

base_url = 'https://baike.baidu.com'
his = ['/item/NBA']

for i in range(50):
    url = base_url + his[-1]

    html = urlopen(url).read().decode('utf-8')
    soup = BeautifulSoup(html, features='lxml')
    print(soup.find("h1").get_text(), '  url: ', his[-1])
    sub_urls = soup.find_all('a', dict(target="_blank", href=re.compile('/item/(%.{2})+$')))

    if len(sub_urls) != 0:
        his.append(random.sample(sub_urls, 1)[0]['href'])
    else:
        # 没有合适的子链接
        his.pop()
