<?xml version="1.0" ?>
- <statefile ts_sec="1343960188" ts_usec="720664">
- <node index="0" name="bridge-sd" from="local">
- <item index="0" name="lagg0" handle="0" level="0" ischild="0" parent="0">
<attr name="bytes" rx="149125974858620" tx="36213321804980" rate_rx="38.56MiB" rate_tx="8.75MiB" />
<attr name="packets" rx="326284113377" tx="262104853003" rate_rx="104.60K" rate_tx="79.33K" />
<attr name="errors" rx="0" tx="81" rate_rx="0" rate_tx="0" />
<attr name="drop" rx="0" tx="0" rate_rx="0" rate_tx="0" />
<attr name="multicast" rx="0" tx="0" rate_rx="0" rate_tx="0" />
<attr name="collisions" rx="0" tx="0" rate_rx="0" rate_tx="0" />
</item>
</node>
</statefile>
我要到有上面这个数据的网页上抓取相应的数据,比如name="bridge-sd",name="lagg0",attr name="bytes" rx="149125974858620" tx="36213321804980" rate_rx="38.56MiB" rate_tx="8.75MiB",attr name="packets" rx="326284113377" tx="262104853003" rate_rx="104.60K" rate_tx="79.33K",然后分相应的字段存入MYSQL,要怎么写,我怎么都抓取不下来~
- <statefile ts_sec="1343960188" ts_usec="720664">
- <node index="0" name="bridge-sd" from="local">
- <item index="0" name="lagg0" handle="0" level="0" ischild="0" parent="0">
<attr name="bytes" rx="149125974858620" tx="36213321804980" rate_rx="38.56MiB" rate_tx="8.75MiB" />
<attr name="packets" rx="326284113377" tx="262104853003" rate_rx="104.60K" rate_tx="79.33K" />
<attr name="errors" rx="0" tx="81" rate_rx="0" rate_tx="0" />
<attr name="drop" rx="0" tx="0" rate_rx="0" rate_tx="0" />
<attr name="multicast" rx="0" tx="0" rate_rx="0" rate_tx="0" />
<attr name="collisions" rx="0" tx="0" rate_rx="0" rate_tx="0" />
</item>
</node>
</statefile>
我要到有上面这个数据的网页上抓取相应的数据,比如name="bridge-sd",name="lagg0",attr name="bytes" rx="149125974858620" tx="36213321804980" rate_rx="38.56MiB" rate_tx="8.75MiB",attr name="packets" rx="326284113377" tx="262104853003" rate_rx="104.60K" rate_tx="79.33K",然后分相应的字段存入MYSQL,要怎么写,我怎么都抓取不下来~
$Contents=file_get_contents($arrurl[2]);
echo aa('#正则表达式,我写不出来#ix', $Contents);
function aa($preg,$Contents,$num=1){
preg_match($preg, $Contents,$arr);
return $arr[$num];
}
我才开始写,想尝试着先提取一个字段的内容,但是,老是匹配不出来。
echo $Contents; //贴出结果
- <statefile ts_sec="1343982515" ts_usec="21645">
- <node index="0" name="bridge-tianjin" from="local">
- <item index="0" name="lagg0" handle="0" level="0" ischild="0" parent="0">
<attr name="bytes" rx="172201952120981" tx="34667621568748" rate_rx="2.29MiB" rate_tx="371.99KiB" />
<attr name="packets" rx="380693940011" tx="302615537460" rate_rx="2.78K" rate_tx="2.25K" />
<attr name="errors" rx="0" tx="17759" rate_rx="0" rate_tx="0" />
<attr name="drop" rx="0" tx="0" rate_rx="0" rate_tx="0" />
<attr name="multicast" rx="0" tx="0" rate_rx="0" rate_tx="0" />
<attr name="collisions" rx="0" tx="0" rate_rx="0" rate_tx="0" />
</item>
</node>
</statefile>
我总共要去8个固定的网页上获取类似的数据。
print_r($sss);试试
<?xml version="1.0" ?>
-<statefile ts_sec="1343960188" ts_usec="720664">
-<node index="0" name="bridge-sd" from="local">
-<item index="0" name="lagg0" handle="0" level="0" ischild="0" parent="0">
<attr name="bytes" rx="149125974858620" tx="36213321804980" rate_rx="38.56MiB" rate_tx="8.75MiB" />
<attr name="packets" rx="326284113377" tx="262104853003" rate_rx="104.60K" rate_tx="79.33K" />
<attr name="errors" rx="0" tx="81" rate_rx="0" rate_tx="0" />
<attr name="drop" rx="0" tx="0" rate_rx="0" rate_tx="0" />
<attr name="multicast" rx="0" tx="0" rate_rx="0" rate_tx="0" />
<attr name="collisions" rx="0" tx="0" rate_rx="0" rate_tx="0" />
</item>
</node>
</statefile>
xml;
$xml=simplexml_load_string(str_replace('-','',$s));
print_r($xml);
SimpleXMLElement Object
(
[@attributes] => Array
(
[ts_sec] => 1343960188
[ts_usec] => 720664
) [node] => SimpleXMLElement Object
(
[@attributes] => Array
(
[index] => 0
[name] => bridgesd
[from] => local
) [item] => SimpleXMLElement Object
(
[@attributes] => Array
(
[index] => 0
[name] => lagg0
[handle] => 0
[level] => 0
[ischild] => 0
[parent] => 0
) [attr] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => bytes
[rx] => 149125974858620
[tx] => 36213321804980
[rate_rx] => 38.56MiB
[rate_tx] => 8.75MiB
) ) [1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => packets
[rx] => 326284113377
[tx] => 262104853003
[rate_rx] => 104.60K
[rate_tx] => 79.33K
) ) [2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => errors
[rx] => 0
[tx] => 81
[rate_rx] => 0
[rate_tx] => 0
) ) [3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => drop
[rx] => 0
[tx] => 0
[rate_rx] => 0
[rate_tx] => 0
) ) [4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => multicast
[rx] => 0
[tx] => 0
[rate_rx] => 0
[rate_tx] => 0
) ) [5] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => collisions
[rx] => 0
[tx] => 0
[rate_rx] => 0
[rate_tx] => 0
) ) ) ) ))
数据成了这样,获取就难不倒你了吧。
$xml = simplexml_load_string($content);
下面是个读取google weather api的例子,可以才考下 //google weather API
$url = 'http://www.google.com/ig/api?weather='.$cty.'&hl=en';
//extract weather data
$content = file_get_contents($url); $xml = simplexml_load_string($content);
$date = $xml->weather->forecast_information->forecast_date['data'];
$city = $xml->weather->forecast_information->city['data'];
$condition = $xml->weather->current_conditions->condition['data'];
$tempF = $xml->weather->current_conditions->temp_f['data'];
$tempC = $xml->weather->current_conditions->temp_c['data'];
$sql="select * from url limit 2";
$q=mysql_query($sql);
while ($row=mysql_fetch_array($q)) {
$Contents=file_get_contents($row[url]);
$sj=simplexml_load_string(str_replace('-','',$Contents));
foreach ($sj[1] as $id=>$v){
$ssql="insert into 'liuliang'('id','ts_sec','ts_usec',
$ssql.='index_name','item_name','b_name','b_rx',b_tx,
$ssql.='b_rate_rx','b_rate_tx','p_name','p_rx','p_tx',
$ssql.='p_rate_rx','p_rate_tx','e_name','e_rx','e_tx',
$ssql.='e_rate_rx','e_rate_tx','d_name','d_rx','d_tx',
$ssql.='d_rate_rx','d_rate_tx','m_name','m_rx','m_tx',
$ssql.='m_rate_rx','m_rate_tx','c_name','c_rx','c_tx',
$ssql.='c_rate_rx','c_rate_tx') values (null,'".$sj[1][$id]."','".$v."');";
mysql_query($ssql);这个是我写的代码