mangy1983 51 Posted September 17, 2011 Report Share Posted September 17, 2011 A wee bit stuck with a code so if anyone in the know might be able to help? cheers Callum Quote Link to post Share on other sites
dymented 2,220 Posted September 17, 2011 Report Share Posted September 17, 2011 what do you need doing bud Quote Link to post Share on other sites
mangy1983 51 Posted September 17, 2011 Author Report Share Posted September 17, 2011 what do you need doing bud Thank goodness buddy tearing my hair out here! I'm trying to screen scrape the top row of information only from this web link: http://www.metoffice.gov.uk/weather/uk/he/stornoway_forecast_weather.html to create my own weather widget as l don't like the colour choices they give you and they don't suite my site. This info would be saved on my own database and updated every 3 hours using a cron job to update the information. I would like it if possible to pick each separate piece of info and place it in a variable but the code l found closest to what l need is this one found here but even this one does not work lol <?php $url = "http://www.nfl.com/teams/sandiegochargers/roster?team=SD"; $raw = file_get_contents($url); $newlines = array("\t","\n","\r","\x20\x20","\0","\x0B"); $content = str_replace($newlines, "", html_entity_decode($raw)); $start = strpos($content,'<table cellpadding="2" class="standard_table"'); $end = strpos($content,'</table>',$start) + 8; $table = substr($content,$start,$end-$start); preg_match_all("|<tr(.*)</tr>|U",$table,$rows); foreach ($rows[0] as $row){ if ((strpos($row,'<th')===false)){ preg_match_all("|<td(.*)</td>|U",$row,$cells); $number = strip_tags($cells[0][0]); $name = strip_tags($cells[0][1]); $position = strip_tags($cells[0][2]); echo "{$position} – {$name} – Number {$number} <br>\n"; } } ?> Quote Link to post Share on other sites
dymented 2,220 Posted September 17, 2011 Report Share Posted September 17, 2011 not with that script your not here its on there site http://www.metoffice.gov.uk/public/pws/components/ just add a block ect if your logged in to your admin panel and past the code yes you can add the html code to a php block Quote Link to post Share on other sites
mangy1983 51 Posted September 17, 2011 Author Report Share Posted September 17, 2011 not with that script your not here its on there site http://www.metoffice...pws/components/ just add a block ect if your logged in to your admin panel and past the code yes you can add the html code to a php block I'm not sure l understand you there mate. Are you saying l should use their official widget or that what l would like to do is against their terms and conditions? cheers Callum Quote Link to post Share on other sites
dymented 2,220 Posted September 17, 2011 Report Share Posted September 17, 2011 what do you need doing bud Thank goodness buddy tearing my hair out here! I'm trying to screen scrape the top row of information only from this web link: http://www.metoffice...st_weather.html to create my own weather widget as l don't like the colour choices they give you and they don't suite my site. This info would be saved on my own database and updated every 3 hours using a cron job to update the information. I would like it if possible to pick each separate piece of info and place it in a variable but the code l found closest to what l need is this one found here but even this one does not work lol <?php $url = "[color=#ff0000]http://www.nfl.com/teams/sandiegochargers/roster?team=SD[/color]"; $raw = file_get_contents($url); $newlines = array("\t","\n","\r","\x20\x20","\0","\x0B"); $content = str_replace($newlines, "", html_entity_decode($raw)); $start = strpos($content,'<table cellpadding="2" class="standard_table"'); $end = strpos($content,'</table>',$start) + 8; $table = substr($content,$start,$end-$start); preg_match_all("|<tr(.*)</tr>|U",$table,$rows); foreach ($rows[0] as $row){ if ((strpos($row,'<th')===false)){ preg_match_all("|<td(.*)</td>|U",$row,$cells); $number = strip_tags($cells[0][0]); $name = strip_tags($cells[0][1]); $position = strip_tags($cells[0][2]); echo "{$position} – {$name} – Number {$number} <br>\n"; } } ?> for a start thats the wrong url bud www.nfl.com ???? Quote Link to post Share on other sites
mangy1983 51 Posted September 17, 2011 Author Report Share Posted September 17, 2011 what do you need doing bud Thank goodness buddy tearing my hair out here! I'm trying to screen scrape the top row of information only from this web link: http://www.metoffice...st_weather.html to create my own weather widget as l don't like the colour choices they give you and they don't suite my site. This info would be saved on my own database and updated every 3 hours using a cron job to update the information. I would like it if possible to pick each separate piece of info and place it in a variable but the code l found closest to what l need is this one found here but even this one does not work lol <?php $url = "[color=#ff0000]http://www.nfl.com/teams/sandiegochargers/roster?team=SD[/color]"; $raw = file_get_contents($url); $newlines = array("\t","\n","\r","\x20\x20","\0","\x0B"); $content = str_replace($newlines, "", html_entity_decode($raw)); $start = strpos($content,'<table cellpadding="2" class="standard_table"'); $end = strpos($content,'</table>',$start) + 8; $table = substr($content,$start,$end-$start); preg_match_all("|<tr(.*)</tr>|U",$table,$rows); foreach ($rows[0] as $row){ if ((strpos($row,'<th')===false)){ preg_match_all("|<td(.*)</td>|U",$row,$cells); $number = strip_tags($cells[0][0]); $name = strip_tags($cells[0][1]); $position = strip_tags($cells[0][2]); echo "{$position} – {$name} – Number {$number} <br>\n"; } } ?> for a start thats the wrong url bud www.nfl.com ???? Ah yes l get you now mate that was the code that was closest to what l was looking for. Usually l implement the code exactly as it is and ensure it is working before l customize it for my own use. The problem was that the code supplied from the tutorial uncustomised does not work Here is where the above code came from: http://www.bradino.com/php/screen-scraping/ thanks Callum Quote Link to post Share on other sites
mangy1983 51 Posted September 17, 2011 Author Report Share Posted September 17, 2011 (edited) One of the lads over at the phpfreaks forum wrote this code for me but the website keeps on going down. It has been down now for over an hour. The code below pretty much does whats asked bar that l want each tds element saved as a variable so that l can upload it to an mysql database. I would run this using a cron job at a 3 hour interval to run in conjunction with the met offices update. The output of the below code can be seen here: http://www.syaa.co.u...obs/Weather.php <?php echo '<style type="text/css"> table { border-collapse: collapse; } table, th, td { border: 1px solid black; padding: 2px; } </style>'; $doc = new DOMDocument(); @$doc->loadHTMLFile('http://www.metoffice.gov.uk/weather/uk/he/stornoway_forecast_weather.html'); //load the file; $desired_rows = 1; //How many rows you want from the table. $table = $doc->getElementsByTagName('table'); //get our tables out, it should return 2 from the file, we only want the second. $rows = $table->item(1)->getElementsByTagName('tr'); //pull the table rows from the second table (notice we select the second by item(1).) $count = $rows->length; //returns a count of the table rows. echo '<table id="weather"><tr> <th rowspan="2">Date</th> <th rowspan="2">Time</th> <th rowspan="2">Weather</th> <th rowspan="2">Temp</th> <th colspan="3">Wind</th> <th rowspan="2">Visibility</th> </tr> <tr> <th>Dir</th> <th>Speed</th> <th>Gust</th> </tr>'; //mock up of the original table headers. for($i=2,$start=$i;$i<($start + $desired_rows);$i++) { //for loop, goes through the rows. echo '<tr>'; //start row. $columns = $rows->item($i)->getElementsByTagName('td'); //get columns for this row. $columnCount = $columns->length; for($n=0;$n<$columnCount;$n++) { //go through the columns. if($n == 2) { $img = $columns->item($n)->getElementsByTagName('img'); //the 3rd column is an image, so we must get the image title. $value = $img->item(0)->getAttribute('title'); } else { $value = $columns->item($n)->nodeValue; //else we will just take what is in the column. } echo '<td>' . $value . '</td>'; //push the column to the screen. } echo '</tr>'; //end the row. } echo '</table>'; //end the table. ?> thanks Callum Edited September 17, 2011 by mangy1983 Quote Link to post Share on other sites
mangy1983 51 Posted September 17, 2011 Author Report Share Posted September 17, 2011 Well don't know if this is the best way but l managed to recode the loop to create incremented variables assigned to each value so that l can now implement them to a database. Here is my completed code if it helps someone and special thanks to anyone who helped me cheers Callum <?php $doc = new DOMDocument(); @$doc->loadHTMLFile('http://www.metoffice.gov.uk/weather/uk/he/stornoway_forecast_weather.html'); //load the file; $desired_rows = 1; //How many rows you want from the table. $table = $doc->getElementsByTagName('table'); //get our tables out, it should return 2 from the file, we only want the second. $rows = $table->item(1)->getElementsByTagName('tr'); //pull the table rows from the second table (notice we select the second by item(1).) $count = $rows->length; //returns a count of the table rows. for($i=2,$start=$i;$i<($start + $desired_rows);$i++) { //for loop, goes through the rows. $columns = $rows->item($i)->getElementsByTagName('td'); //get columns for this row. $columnCount = $columns->length; for($n=0;$n<$columnCount;$n++) { //go through the columns. if($n == 2) { $img = $columns->item($n)->getElementsByTagName('img'); //the 3rd column is an image, so we must get the image title. $value = $img->item(0)->getAttribute('title'); } else { $value = $columns->item($n)->nodeValue; //else we will just take what is in the column. } ${a.$n} = $value; } } echo $a0, '</br>', $a1, '</br>', $a2, '</br>', $a3, '</br>', $a4, '</br>', $a5, '</br>', $a6, '</br>', $a7, '</br>', $a8; ?> Quote Link to post Share on other sites
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.
Note: Your post will require moderator approval before it will be visible.