我试图弄清楚如何从表中打印所有tr元素,但是我不能完全正常工作。这是我正在使用的链接。https://en.wikipedia.org/wiki/List_of_current_members_of_the_United_States_Senate这是我的代码。import requestsfrom bs4 import BeautifulSouplink = "https://en.wikipedia.org/wiki/List_of_current_members_of_the_United_States_Senate"html = requests.get(link).text# If you do not want to use requests then you can use the following code below# with urllib (the snippet above). It should not cause any issue."""soup = BeautifulSoup(html, "lxml")res = soup.findAll("span", {"class": "fn"})for r in res: print("Name: " + r.find('a').text) table_body=soup.find('senators') rows = table_body.find_all('tr') for row in rows: cols=row.find_all('td') cols=[x.text.strip() for x in cols] print(cols)我正在尝试打印tr名为的表中的所有元素'senators'。另外,我想知道是否有一种方法可以点击参议员的链接,就像'Richard Shelby'这样将我带到这里:https://zh.wikipedia.org/wiki/理查德·谢尔比从每个链接,我想抓取'Assumed office'. 在这种情况下,该值为:'January 3, 2018'。因此,最终,我想得出以下结论:Richard Shelby May 6, 1934 (age 84) Lawyer U.S. HouseAlabama Senate January 3, 1987 2022Assumed office: January 3, 2018我现在能得到的是印出的每个参议员的名字。
添加回答
举报
0/150
提交
取消