1 # 上行遍历 2 soup = BeautifulSoup(demo, 'html.parser') 3 for parent in soup.a.parents: 4 if parent is None: 5 print(parent) 6 else: 7 print(parent.name)