Sunday 17 August 2014

Scraping data from National weather services

Hi I want to get the latest weather data from the link below, meaning the very first row of data, I am trying to scrape the data but have not been successful in doing so. Can anyone please help, or if you know any better way point me to that direction thanks!

this is what i have,

$data = file_get_contents('http://w1.weather.gov/obhistory/KIKV.html');

$regex = '/<td>(.+?) </td>/';

preg_match($regex,$data,$match);

var_dump($match);

echo $match[1];

i get an error on '/(.+?) /' this line that "Unknown modifier 't'" ? Any suggestion

1 Answer

Although parsing html with a regex is usually a very bad idea, the error you are receiving, is caused by the / character. You are using that character as the delimiter for your pattern, so you need to escape it or use another character:

$regex = '/<td>(.+?) <\/td>/';

or
$regex = '#<td>(.+?) </td>#';

Source:http://stackoverflow.com/questions/11943420/scraping-data-from-national-weather-services

No comments:

Post a Comment