===========
from bs4 import BeautifulSoup
import re
import random
import requests
from urllib.request import urlopen
base_url = "https://baike.baidu.com"
his = ["/item/%E7%BD%91%E7%BB%9C%E7%88%AC%E8%99%AB"]
for i in range(3):
# dealing with Chinese symbols
url = base_url + his[-1]
html = urlopen(url).read().decode('utf-8')
#html = requests.get(url)
soup = BeautifulSoup(html, features='lxml')
print(i, soup.find('h1').get_text(), ' url: ', his[-1])
# find valid urls
sub_urls = soup.find_all("a", {"target": "_blank", "href": re.compile("/item/(%.{2})+$")})
# •. : 匹配任何字符 (除了 \n)
# {n} : 重复 n 次
if len(sub_urls) != 0:
his.append(random.sample(sub_urls, 1)[0]['href'])
else:
# no valid sub link found
his.pop()