jdashca Posted September 14, 2008 Share Posted September 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-640969 Share on other sites More sharing options...
thebadbad Posted September 14, 2008 Share Posted September 14, 2008 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). Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-641008 Share on other sites More sharing options...
thebadbad Posted September 14, 2008 Share Posted September 14, 2008 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) ?> Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-641018 Share on other sites More sharing options...
jdashca Posted September 14, 2008 Share Posted September 14, 2008 Something is not right. The first item in the array is an incorrect game, and subsequent games have the wrong gamestatus... Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-641345 Share on other sites More sharing options...
jdashca Posted September 15, 2008 Share Posted September 15, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-641601 Share on other sites More sharing options...
jdashca Posted September 20, 2008 Share Posted September 20, 2008 Any idea why the code works for week 1 but not week 2? Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-646248 Share on other sites More sharing options...
jake8 Posted September 22, 2008 Share Posted September 22, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-647733 Share on other sites More sharing options...
thebadbad Posted September 22, 2008 Share Posted September 22, 2008 Anyone interested in the final (extensive) script, PM me. Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-647750 Share on other sites More sharing options...
skaforey Posted November 8, 2009 Share Posted November 8, 2009 Sorry to bump an extremely old post, but does anyone have the source for the final script? I sent a PM to the original author but have not received a response. It would be very much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-953672 Share on other sites More sharing options...
thebadbad Posted November 8, 2009 Share Posted November 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-953683 Share on other sites More sharing options...
PFMaBiSmAd Posted November 8, 2009 Share Posted November 8, 2009 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] Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-953704 Share on other sites More sharing options...
skaforey Posted November 8, 2009 Share Posted November 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-953771 Share on other sites More sharing options...
w2swr Posted October 9, 2012 Share Posted October 9, 2012 I like your script very much. could you PM me the final copy? The script is working with the exception of the first game on the list. No date and wrong awayteam. Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-1383898 Share on other sites More sharing options...
Hacym Posted October 9, 2012 Share Posted October 9, 2012 You realize that you resurrected a topic from 2008, right? Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-1383910 Share on other sites More sharing options...
w2swr Posted October 9, 2012 Share Posted October 9, 2012 Yea, I figured I would give it a shot. It still works and I cant find anything else like it. Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-1383914 Share on other sites More sharing options...
w2swr Posted October 9, 2012 Share Posted October 9, 2012 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=" Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-1383922 Share on other sites More sharing options...
Maq Posted October 9, 2012 Share Posted October 9, 2012 (edited) Why don't you use an API instead of screen scraping like a barbarian? There are plenty of free and pay for ones, for example - http://developer.espn.com/docs/scores. Edited October 9, 2012 by Maq Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-1383940 Share on other sites More sharing options...
premiso Posted October 9, 2012 Share Posted October 9, 2012 (edited) 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 October 9, 2012 by premiso Quote Link to comment https://forums.phpfreaks.com/topic/121464-grabbing-nfl-scores/page/2/#findComment-1383962 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.