【发布时间】:2014-03-03 10:21:36
【问题描述】:
我正在抓取一个气象站的天气信息并尝试将其转换为数据库格式。
我从网页上抓取了我需要的数据,但是当我尝试替换代码中的所有匹配项时,它只执行一次,而且我仍然不知道如何将其转换为数组。我正在尝试做的数组类似于(日期、时间、变量、值)。稍后我需要将数组放入 .txt 文件和数据库中。
这是我到目前为止所得到的:
<?php;
function scrape_between($data, $start, $end){
$data = stristr($data, $start); // miro que quito todo antes de start
$data = substr($data, strlen($start)); // Stripping $start
$stop = stripos($data, $end); // Getting the position of the $end of the data to scrape
$data = substr($data, 0, $stop); // Stripping all data from after and including the $end of the data to scrape
return $data; // devuelvo data entre las sentencias
}
// Defining the basic cURL function
$url = 'http://aprs.fi/?c=raw&call=EA4RKU-13&limit=1000&view=normal';
$ch = curl_init(); // Initialising cURL
curl_setopt($ch, CURLOPT_URL, $url); // Setting cURL's URL option with the $url variable passed into the function
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data
$data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
$start="<div class='browselist_data'>";
$end = '</div>';
$data = scrape_between($data, $start, $end); // me quedo con la parte con span
curl_close($ch); // Closing cURL
//echo strip_tags($data); // quita todos los tags html y php deja feo
$z = count($data);
echo "numero de datos: " .$z; //echo $data; // Returning the data from the function
$data1=preg_replace('/<b>.+?<\/b>/im', '' ,$data);
//$data1=preg_replace('/<b>.+?<\/b>/im', '' ,$data);
$data1=preg_replace('/$<a>.+?<\/a>/', '', $data1);
echo $data1;
?>
【问题讨论】:
-
仅供参考,这个词是“刮擦”,而不是“刮擦”——我已经提交了一个修改来改变它。