Jump to content

grabbing NFL scores


chiefrokka

Recommended Posts

Sorry I wasn't more clear. I know week 2 hasn't started; I'd still expect the array to be filled with the games that are yet to be played, but simply with a different status. Reading your reply now, I better understand the structure of the code. Your help has been awesome.

Link to comment
Share on other sites

Good :) I would have to alter the script, to make it grab upcoming games also, since the source code is different with these games. Maybe I'll give it a try, should be doable.

 

Remember if you use this script, it would be a good idea to cache the results, and update it with intervals, instead of running the script each time a page is loaded (= faster, saves server load).

Link to comment
Share on other sites

I think I got it, had to grab upcoming games separately, and append them to the $scores array. Hope it works when theres both finished and upcoming games on same page (it really should).

 

<?php
//set current game week
$Current_Week = 2;

//load source code, depending on the current week, of the website into a variable as a string
$url = "http://sports.yahoo.com/nfl/scoreboard?w=$Current_Week";
$string = file_get_contents($url);

//search and store game dates, later linked to the games
preg_match_all('~<td colspan="3" height="18" class="yspdetailttl"> (.*?)</td>~is', $string, $matches);
$matches = array_map('trim', $matches[1]);
$pos_arr = array();
foreach ($matches as $date) {
$pos_arr[strpos($string, $date)] = $date;
}

//set search pattern (using regular expressions)
$find = '~<a href="/nfl/teams/.*?">(.*?)</a>.*?<td align="right" class="ysptblclbg6 total">.*?<span class="yspscores">(.*?) </span>.*?</td>.*?<td align="right" class="ysptblclbg6 total"><span class="yspscores">(.*?)</span>~is';

//search the string for the pattern, and store the content found inside the set of parens in the array $matches
//$matches[1] is going to hold team names in the order they appear on the page, and $matches[2] the scores
preg_match_all($find, $string, $matches);

//initiate scores array, to group teams and scores together in games
$scores = array();

//count number of teams found, to be used in the loop below
$count = count($matches[1]);

//loop from 0 to $count, in steps of 2
//this is done in order to group 2 teams and 2 scores together in games, with each iteration of the loop
//trim() is used to trim away any whitespace surrounding the team names and scores
//strip_tags() is used to remove the HTML bold tag (<b>) from the winning scores
for ($i = 0; $i < $count; $i += 2) {
if (!ctype_digit(strip_tags(trim($matches[2][$i])))) {continue;}
$away_team = trim($matches[1][$i]);
//associate game dates with games, by checking which date the game appears after
$pos = strpos($string, $away_team . '</a>');
foreach ($pos_arr as $datepos => $date) {
	if ($pos > $datepos) {
		$game_date = $date;
	}
}
$away_score = trim($matches[2][$i]);
$home_team = trim($matches[1][$i + 1]);
$home_score = trim($matches[2][$i + 1]);
$winner = (strpos($away_score, '<') === false) ? $home_team : $away_team;
$game_status = $matches[3][$i];
$scores[] = array(
	'gamedate' => $game_date,
	'gamestatus' => $game_status,
	'awayteam' => $away_team,
	'awayscore' => strip_tags($away_score),
	'hometeam' => $home_team,
	'homescore' => strip_tags($home_score),
	'winner' => ($game_status == 'Final') ? $winner : '-'
);
}

//grab upcoming games

$find = '~<b><a href="/nfl/teams/.+?">(.+?)</a></b>~is';
preg_match_all($find, $string, $matches);
$count = count($matches[1]);

for ($i = 0; $i < $count; $i += 2) {
$away_team = trim($matches[1][$i]);
//associate game dates with games, by checking which date the game appears after
$pos = strpos($string, $away_team . '</a></b>');
foreach ($pos_arr as $datepos => $date) {
	if ($pos > $datepos) {
		$game_date = $date;
	}
}
$home_team = trim($matches[1][$i + 1]);
$scores[] = array(
	'gamedate' => $game_date,
	'gamestatus' => '-',
	'awayteam' => $away_team,
	'awayscore' => '-',
	'hometeam' => $home_team,
	'homescore' => '-',
	'winner' => '-'
);
}

//see how the scores array looks
echo '<pre>' . print_r($scores, true) . '</pre>';

//game standings, game statuses and winning teams (of finished games) can now be accessed from the scores array
//e.g. $scores[0]['awayteam'] contains the name of the away team (['awayteam'] part) from the first game on the page ([0] part)
?>

Link to comment
Share on other sites

To be more specific, the first item in the array shows a empty gamedate, and shows Baltimore playing Cincinnati. Baltimore has a bye this week. It should indicate that Tennessee is at Cincinnati. While the game is now final, when it was underway, the incorrect gamestatus was also shown.

Link to comment
Share on other sites

Make sure you changed the Current_Week variable to 2 or now 3.

 

Nice piece of work; however, I noticed that for Week 3, the first game (Kansas City vs. Atlanta) does not show up.  I ran the last version of code posted, changed to week 3 and 14 out of 15 games show up.  The Kansas City vs. Atlanta does not.  The Jet/Charger game also does not appear is that because the game has not started/finished?

Link to comment
Share on other sites

  • 1 year later...

Here is a semi-automated (you need to copy/paste from the browser's screen) method that works with the current season at http://www.nfl.com/schedules

 

<?php
// parse wxresults.txt, x = 1,2,3,4... files (copy/paste from http://www.nfl.com/schedules)
// find only lines with @ in them, then find the teams and score before and after the @. Lookup abriviation to get full name.
// display the 'form' with the winning teams selected. For demo, submits and adds a line to the picks.txt file showing the winning teams.
// in a real application the picks, winning teams, and the winning picks would be handled through a database.

$week = isset($_GET['week']) ? (int)$_GET['week'] : 1;
$file = "w{$week}results.txt";

$lookup = array();
$lookup['CAR'] = 'Carolina Panthers';
$lookup['ATL'] = 'Atlanta Falcons';
$lookup['NO'] = 'New Orleans Saints';
$lookup['PHI'] = 'Philadelphia Eagles';
$lookup['STL'] = 'St. Louis Rams';
$lookup['WAS'] = 'Washington Redskins';
$lookup['HOU'] = 'Houston Texans';
$lookup['TEN'] = 'Tennessee Titans';
$lookup['NE'] = 'New England Patriots';
$lookup['NYJ'] = 'New York Jets';
$lookup['CIN'] = 'Cincinnati Bengals';
$lookup['GB'] = 'Green Bay Packers';
$lookup['ARI'] = 'Arizona Cardinals';
$lookup['JAC'] = 'Jacksonville Jaguars';
$lookup['OAK'] = 'Oakland Raiders';
$lookup['KC'] = 'Kansas City Chiefs';
$lookup['MIN'] = 'Minnesota Vikings';
$lookup['DET'] = 'Detroit Lions';
$lookup['TB'] = 'Tampa Bay Buccaneers';
$lookup['BUF'] = 'Buffalo Bills';
$lookup['SEA'] = 'Seattle Seahawks';
$lookup['SF'] = 'San Francisco 49ers';
$lookup['PIT'] = 'Pittsburgh Steelers';
$lookup['CHI'] = 'Chicago Bears';
$lookup['BAL'] = 'Baltimore Ravens';
$lookup['SD'] = 'San Diego Chargers';
$lookup['CLE'] = 'Cleveland Browns';
$lookup['DEN'] = 'Denver Broncos';
$lookup['NYG'] = 'New York Giants';
$lookup['DAL'] = 'Dallas Cowboys';
$lookup['IND'] = 'Indianapolis Colts';
$lookup['MIA'] = 'Miami Dolphins';

// the following common code is used by the form code and partially by the form processing code
$teams = array(); // used by form code
$lines = file($file); // read lines from the file into an array
$i = 1; // counter for win array
$win = array(); // array to hold winners (for form code)
foreach($lines as $line){
// find only lines with @ in them
$pos = strpos($line,'@');
if($pos !== FALSE){
	// get only the portion before the first \t
	$part = explode("\t",$line);
	// get each team and score using preg_split $keywords = preg_split("/[\s,]+/", "hypertext language, programming");
	//list($teamA,$scoreA,$teamB,$scoreB) = preg_split("/[\s]+/", $part[0]);
	$results = preg_split("/[\s]+/", $part[0]);
	//echo "<pre>";
	//var_dump($results);
	//echo "</pre>";
	// 0 = teamA, 1 = scoreA, 3 = teamB, 4 = scoreB
	// put teams into array to be used by the form code
	$teams[] = $lookup[$results[0]];
	$teams[] = $lookup[$results[3]];
	if($results[1] > $results[4]){
		$win[$i] = $lookup[$results[0]]; // teamA won
	} else {
		$win[$i] = $lookup[$results[3]]; // teamB won
	}
	$i++;
}
}
$total_games = count($teams) / 2;

// condition inputs/default values
$submitted = isset($_POST['submit']) ? $_POST['submit'] : "";

// form processing code
if($submitted){
// validate inputs
$form_errors = array();
// check for empty name
if(empty($_POST["name"])){
	$form_errors['name_field'] = "Fill in your name:";
}
// check for a pick in each game pair
for($i=1; $i <= $total_games;$i++){
	if(!isset($_POST['pick'][$i])){
		$form_errors["pick_{$i}"] = "Select one:";
	}
}

// process the data if no errors
if(empty($form_errors)){
	$myFile = "Picks{$week}.txt";
	$fh = fopen($myFile, 'a') or die("can't open file");
	fwrite($fh, "Week{$week},{$_POST['name']},");
	fwrite($fh, implode(',', $_POST['pick']));
	fwrite($fh, "\n");
	fclose($fh);
	Echo "Your picks have been recorded!";
}

} // end of the form processing code

// form code
if(!$submitted || !empty($form_errors)){
$error_message = empty($form_errors) ? "" : "Please correct the errors shown in red -";
?>
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title>Picks</title>
</head>
<body>
<p><?php echo "Enter your Name and select your picks for Week $week, then submit the form."; ?> </p>
<span style='color:red'><?php echo $error_message; ?></span>
<form method="post" action="#first">
<label for="name">Name: <?php echo isset($form_errors['name_field']) ? "<span style='color:red'>{$form_errors['name_field']}</span>" : "";?></label><br />
<input type="text" name="name" value="***WINNERS***"><br />
<?php
$counter = 0; // keep track of pairs of teams
$game = 1; // keep track of games (for radio buttons)
$first_error = true;
foreach($teams as $team){
if($counter % 2 == 0){
	if(isset($form_errors["pick_{$game}"])){
		if($first_error){
			echo "<a name='first'></a>";
			$first_error = false;
		}
		echo "<span style='color:red'>{$form_errors["pick_{$game}"]}<br /></span>";
	}
}
//$class = (isset($form_errors["pick_{$game}"])) ? "red" : "black";
//checked="checked" $_POST['pick'][$game] if isset, will match a value
$selected = "";
if(isset($win[$game]) && $win[$game] == $team){
	$selected = "checked='checked'";
}
//echo "<input name='pick[$game]' value='$team' type='radio' $selected><span style='color:$class'> $team</span><br>\n";
echo "<input name='pick[$game]' value='$team' type='radio' $selected> $team<br />\n";
$counter++;
if($counter % 2 == 0){
	$game++;
	echo "<br>\n";
}
}
?>
  <input name="submit" value="Submit Picks!" type="submit"> <input type="reset"></form>
<?php } // end of the form code ?>
</body>
</html>

 

This produces a form (similar to the form that would be used to pick the choices for the games) and allows them to be submitted so that you can do anything with the information that you need. You could of course skip the form and use the results directly.

 

You specify the week number on the end of the URL yourfile.php?week=8

 

The wxresults.txt file is the text that copy/pasted by dragging your mouse over the correct rows of text at http://www.nfl.com/schedules

 

I have attached the week 8 results.txt file to this post as an example -

 

[attachment deleted by admin]

Link to comment
Share on other sites

I did see your PM, but the problem is that the script doesn't work for this season, and that I haven't had the time to fix it/rewrite it yet. But I may come around doing it at some point.

 

Would you mind posting the "latest" code even if it doesn't work? I have some time and would be happy to try to fix it and post results if I can get it to work.

 

@PFMaBiSmAd, thanks for input, however, I am looking for something fully automated.

 

 

Link to comment
Share on other sites

  • 2 years later...

Actually I may of fixed the problem. We shall see if it works while a game is in play. I added a <b> in the beginning of the search pattern

//set search pattern (using regular expressions)
$find = '~[b][color=#ff0000]<b>[/color][/b]<a href="

Link to comment
Share on other sites

Scraping the data to be utilized else other (non-personal) is illegal for anyone who cares. Do as Maq suggests and get an API setup and be legit.

 

/me votes to close this ancient thread.

Edited by premiso
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.