-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFLStatsScraper.java
188 lines (159 loc) · 5.64 KB
/
NFLStatsScraper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Element;
// program to scrape 2018-2019 NFL statistical information
// using nfl.com
public class NFLStatsScraper {
private JTextField in, out; // our text boxes
private String mode = "RUSHING";
private JFrame frame;
private final int WIDTH = 400, HEIGHT = 250;
public static void main(String args[]){
new NFLStatsScraper();
}
// sets up the graphics
public NFLStatsScraper() {
// sets up our 3 buttons in a panel
JPanel buttonPanel = new JPanel();
JButton rushButton = new JButton("Rushing");
rushButton.setForeground(Color.red);
JButton passButton = new JButton("Passing");
JButton recButton = new JButton("Receiving");
rushButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mode = "RUSHING";
rushButton.setForeground(Color.red);
recButton.setForeground(Color.black);
passButton.setForeground(Color.black);
}
});
passButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mode = "PASSING";
rushButton.setForeground(Color.black);
recButton.setForeground(Color.black);
passButton.setForeground(Color.red);
}
});
recButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mode = "RECEIVING";
rushButton.setForeground(Color.black);
recButton.setForeground(Color.red);
passButton.setForeground(Color.black);
}
});
buttonPanel.add(rushButton);
buttonPanel.add(passButton);
buttonPanel.add(recButton);
buttonPanel.setPreferredSize(new Dimension(WIDTH,50));
// sets up the input text box
in = new JTextField();
in.setEditable(true);
in.setPreferredSize(new Dimension(WIDTH,100));
in.setText("Enter a Name");
in.setForeground(Color.gray);
// when enter is pressed, run the web search
in.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_ENTER)
getInfo(in.getText(), 1);
}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
});
// clear the prompt when box is clicked on
in.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
in.setText("");
in.setForeground(Color.black);
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
});
// sets up the output text box
out = new JTextField();
out.setEditable(false);
out.setPreferredSize(new Dimension(WIDTH,100));
// sets up the container panel
JPanel panel = new JPanel();
BoxLayout boxlayout = new BoxLayout(panel, BoxLayout.Y_AXIS);
panel.setLayout(boxlayout);
panel.setBorder(BorderFactory.createTitledBorder("NFL Stats"));
panel.add(out);
panel.add(in);
panel.add(buttonPanel);
// frame setup
frame = new JFrame();
frame.setSize(WIDTH,HEIGHT);
frame.setResizable(false);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(300, 200);
frame.setVisible(true);
}
// scrapes data from the webpage
public void getInfo(String name, int pageNum) {
// changes the text boxes when searching
in.setText("");
out.setText("Searching database...");
out.update(out.getGraphics());
try {
// gets the webpage, using the current page number and stat mode
Document doc = Jsoup.connect("http://www.nfl.com/stats/categorystats?tabSeq=0&season=2018&seasonType=REG&d-447263-p="
+pageNum+"&statisticCategory="+mode+"&qualified=true").get();
// get first (only) table
Element table = doc.select("table").get(0);
// get (all) the rows from the table
Elements rows = table.select("tr");
// go through the rows
for (Element row : rows) {
// cols will hold each piece of information going across the row
Elements cols = row.select("td");
// make sure this row wasn't empty
if (cols.size() > 1) {
// player name is found in the first column
String name2 = cols.get(1).text();
// checks if current row is the player we're looking for
if (name2.equalsIgnoreCase(name) || name2.toLowerCase().contains(
name.toLowerCase())) {
// columns 6 and 9 hold our rushing information
if (mode.equals("RUSHING"))
out.setText(name2 + ": " + cols.get(6).text()+ " rushing yds, " + cols.get(9).text()+" tds");
// columns 8, 11, and 12 hold our rushing information
else if (mode.equals("PASSING"))
out.setText(name2 + ": " + cols.get(8).text()+ " passing yds, " + cols.get(11).text()+" tds, " + cols.get(12).text()+" ints");
// columns 5 and 9 hold our rushing information
else
out.setText(name2 + ": " + cols.get(5).text()+ " receiving yds, " + cols.get(9).text()+" tds");
return; // no need to keep searching
}
}
}
}
// if we go past the last page, we'll get an error
catch (Exception e) {
out.setText("Player not found");
return;
}
// if the player wasn't found on the current page, go to the next one
getInfo(name, pageNum+1);
}
}