英文:
Scraping third table on webpage using Beautiful Soup
问题
我正在尝试使用Beautiful Soup从网页中提取第三个表格,但我只能提取第一个表格:
网页:https://www.pro-football-reference.com/coaches/ReidAn0.htm#all_coaching_history
加载网页的代码是正确的,但问题出在这里:
代码:
table2=soup2.find('table', id="coaching_history")
这一行没有提取任何数据。如果我使用第一个表格的ID,即"id = 'coaching_results'",它可以正常工作。问题仅出现在之后的任何表格。
提前感谢。
英文:
I am trying to scrape the third table from a webpage using beautiful soup but the only one I can extract is the first one:
Webpage: https://www.pro-football-reference.com/coaches/ReidAn0.htm#all_coaching_history
The code to load int the webpage is fine but there is where the issue is
Code:
table2=soup2.find('table', id="coaching_history")
That line isn't pulling any data. If i use the ID from the first table where id = "coaching_results" It works fine. It is just any table after that.
Thanks in advance
答案1
得分: 0
这是获取数据的一种方法:
import pandas as pd
import requests
url = 'https://www.pro-football-reference.com/coaches/ReidAn0.htm#all_coaching_history'
wanted_table = pd.read_html(requests.get(url).text.replace('<!--', '').replace('-->', ''))[2]
print(wanted_table)
终端中的结果:
Year Age Level Employer Role
0 1982 24 College (FBS) Brigham Young Graduate Assistant
1 1983 25 College (NCAA D-II) San Francisco State Offensive Coordinator/Offensive Line
2 1984 26 College (NCAA D-II) San Francisco State Offensive Coordinator/Offensive Line
3 1985 27 College (NCAA D-II) San Francisco State Offensive Coordinator/Offensive Line
4 1986 28 College (FCS) Northern Arizona Offensive Line
5 1987 29 College (FBS) UTEP Offensive Line
6 1988 30 College (FBS) UTEP Offensive Line
7 1989 31 College (FBS) Missouri Offensive Line
8 1990 32 College (FBS) Missouri Offensive Line
9 1991 33 College (FBS) Missouri Offensive Line
10 1992 34 NFL Green Bay Packers Tight Ends/Asst. Offensive Line
11 1993 35 NFL Green Bay Packers Tight Ends/Asst. Offensive Line
12 1994 36 NFL Green Bay Packers Tight Ends/Asst. Offensive Line
13 1995 37 NFL Green Bay Packers Tight Ends/Asst. Offensive Line
14 1996 38 NFL Green Bay Packers Tight Ends/Asst. Offensive Line
15 1997 39 NFL Green Bay Packers Quarterbacks
16 1998 40 NFL Green Bay Packers Quarterbacks
17 1999 41 NFL Philadelphia Eagles Head Coach
18 2000 42 NFL Philadelphia Eagles Head Coach
19 2001 43 NFL Philadelphia Eagles Head Coach
20 2002 44 NFL Philadelphia Eagles Head Coach
21 2003 45 NFL Philadelphia Eagles Head Coach
22 2004 46 NFL Philadelphia Eagles Head Coach
23 2005 47 NFL Philadelphia Eagles Head Coach
24 2006 48 NFL Philadelphia Eagles Head Coach
25 2007 49 NFL Philadelphia Eagles Head Coach
26 2008 50 NFL Philadelphia Eagles Head Coach
27 2009 51 NFL Philadelphia Eagles Head Coach
28 2010 52 NFL Philadelphia Eagles Head Coach
29 2011 53 NFL Philadelphia Eagles Head Coach
30 2012 54 NFL Philadelphia Eagles Head Coach
31 2013 55 NFL Kansas City Chiefs Head Coach
32 2014 56 NFL Kansas City Chiefs Head Coach
33 2015 57 NFL Kansas City Chiefs Head Coach
34 2016 58 NFL Kansas City Chiefs Head Coach
35 2017 59 NFL Kansas City Chiefs Head Coach
36 2018 60 NFL Kansas City Chiefs Head Coach
37 2019 61 NFL Kansas City Chiefs Head Coach
38 2020 62 NFL Kansas City Chiefs Head Coach
39 2021 63 NFL Kansas City Chiefs Head Coach
40 2022 64 NFL Kansas City Chiefs Head Coach
英文:
Here is one way to get that data:
import pandas as pd
import requests
url= 'https://www.pro-football-reference.com/coaches/ReidAn0.htm#all_coaching_history'
wanted_table = pd.read_html(requests.get(url).text.replace('<!--', '').replace('-->', ''))[2]
print(wanted_table)
Result in terminal:
Year Age Level Employer Role
0 1982 24 College (FBS) Brigham Young Graduate Assistant
1 1983 25 College (NCAA D-II) San Francisco State Offensive Coordinator/Offensive Line
2 1984 26 College (NCAA D-II) San Francisco State Offensive Coordinator/Offensive Line
3 1985 27 College (NCAA D-II) San Francisco State Offensive Coordinator/Offensive Line
4 1986 28 College (FCS) Northern Arizona Offensive Line
5 1987 29 College (FBS) UTEP Offensive Line
6 1988 30 College (FBS) UTEP Offensive Line
7 1989 31 College (FBS) Missouri Offensive Line
8 1990 32 College (FBS) Missouri Offensive Line
9 1991 33 College (FBS) Missouri Offensive Line
10 1992 34 NFL Green Bay Packers Tight Ends/Asst. Offensive Line
11 1993 35 NFL Green Bay Packers Tight Ends/Asst. Offensive Line
12 1994 36 NFL Green Bay Packers Tight Ends/Asst. Offensive Line
13 1995 37 NFL Green Bay Packers Tight Ends/Asst. Offensive Line
14 1996 38 NFL Green Bay Packers Tight Ends/Asst. Offensive Line
15 1997 39 NFL Green Bay Packers Quarterbacks
16 1998 40 NFL Green Bay Packers Quarterbacks
17 1999 41 NFL Philadelphia Eagles Head Coach
18 2000 42 NFL Philadelphia Eagles Head Coach
19 2001 43 NFL Philadelphia Eagles Head Coach
20 2002 44 NFL Philadelphia Eagles Head Coach
21 2003 45 NFL Philadelphia Eagles Head Coach
22 2004 46 NFL Philadelphia Eagles Head Coach
23 2005 47 NFL Philadelphia Eagles Head Coach
24 2006 48 NFL Philadelphia Eagles Head Coach
25 2007 49 NFL Philadelphia Eagles Head Coach
26 2008 50 NFL Philadelphia Eagles Head Coach
27 2009 51 NFL Philadelphia Eagles Head Coach
28 2010 52 NFL Philadelphia Eagles Head Coach
29 2011 53 NFL Philadelphia Eagles Head Coach
30 2012 54 NFL Philadelphia Eagles Head Coach
31 2013 55 NFL Kansas City Chiefs Head Coach
32 2014 56 NFL Kansas City Chiefs Head Coach
33 2015 57 NFL Kansas City Chiefs Head Coach
34 2016 58 NFL Kansas City Chiefs Head Coach
35 2017 59 NFL Kansas City Chiefs Head Coach
36 2018 60 NFL Kansas City Chiefs Head Coach
37 2019 61 NFL Kansas City Chiefs Head Coach
38 2020 62 NFL Kansas City Chiefs Head Coach
39 2021 63 NFL Kansas City Chiefs Head Coach
40 2022 64 NFL Kansas City Chiefs Head Coach
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论