#print(soup.find('a').get_text(),'\n后缀:',city[0]) #找到 属性href是/train/开头 的 a标签 province = soup.find_all('a',{"href":re.compile("/train/")}) deffun(url): result='' try: result = urlopen(url,timeout=10).read() except: result = fun(url) print('error') return result print(province) for i in province[1:-1]: print(i['href']) url = base_url+i['href'] html = fun(url) soup = BeautifulSoup(html,features='lxml') #读每个省里的城市 city = soup.find_all('a',{"href":re.compile(i['href'])}) #flag这个省 flag = i['href'] for i in city[1:-1]: #哈哈。终于读到时刻表了 print(i['href']) url = base_url+i['href'] html = fun(url) soup = BeautifulSoup(html,features='lxml') train = soup.find_all('tr') p=re.compile(r'13:14') for i in train: if p.search(str(i)): if i.text.split()[3] == '13:14': answer.append(i.text)