红糖糍粑
您需要的 HTML 位于注释内,因此在soup. 您需要首先获取注释,然后将其解析为新soup对象。然后您可以从中找到tr和th元素。例如:from bs4 import BeautifulSoup, Commentimport requestsres = requests.get('https://www.pro-football-reference.com/players/D/DonaAa00.htm')soup = BeautifulSoup(res.text, 'html.parser')div = soup.find('div', {'id':'all_detailed_defense'})comment_html = div.find(string=lambda text: isinstance(text, Comment))comment_soup = BeautifulSoup(comment_html, 'html.parser')for tr in comment_soup.find_all('tr'): row = [td.text for td in tr.find_all(['td', 'th'])] print(row)给你:['', 'Games', 'Pass Coverage', 'Pass Rush', 'Tackles']['Year', 'Age', 'Tm', 'Pos', 'No.', 'G', 'GS', 'Int', 'Tgt', 'Cmp', 'Cmp%', 'Yds', 'Yds/Cmp', 'Yds/Tgt', 'TD', 'Rat', 'DADOT', 'Air', 'YAC', 'Bltz', 'Hrry', 'QBKD', 'Sk', 'Prss', 'Comb', 'MTkl', 'MTkl%']['2018*+', '27', 'LAR', 'DT', '99', '16', '16', '0', '1', '0', '0.0%', '0', '', '0.0', '0', '39.6', '-2.0', '0', '0', '0', '30', '19', '20.5', '70', '59', '6', '9.2%']['2019*+', '28', 'LAR', 'DT', '99', '16', '16', '0', '0', '0', '', '0', '', '', '0', '', '', '0', '0', '0', '32', '9', '12.5', '55', '48', '6', '11.1%']