php怎么抓取这个链接https://locate.apple.com/cn/zh/service/?pt=3&lat=23.134521&lon=113.358803的源码

访问这个链接查看源码很多东西.类似window.resourceLocator.storeSetup = {"defaultChannel":"service","results":[{"storeName":"北京直信创邺-广州天河区店","storeURL":"","storeEmail":"GSX-TH at sogservice.com.cn","phoneNumber":"020-38889171","distance":"2.8 km","locationData":{"streetAddress1":"广东省广州市天河区天河路490号壬丰大厦(西厅)17楼1703室","streetAddress2":"","city":"广州","state":"广东","postalCode":"510630","country":"CN","district":"天河区","regionName":"这样的信息.
怎么抓取源码并截取字符串得到以下这样格式的信息呢

[北京直信创邺-广州天河区店2.4 km
广东省广州市天河区天河路490号壬丰大厦(西厅)17楼1703室
广州, 广东 510630
020-38889171]
我试了 file()函数 file_get_contents()函数 curl等方式都无法抓取.请教高手们!谢谢~~100财富太少了!但最多只能设置这个.帮忙解决这个问题1700财富全给了.这个问题困扰了我很久怎么抓取都是空的.呜呜~~~

<?php
function dg_string($data,$flagA, $flagB, $start = 0){//配套截取字符串
$flagAL=strlen($flagA);
$flagBL=strlen($flagB);
$rn='';
$a=$b=0;
if(($findA=strpos($data,$flagA, $start))!==false){
$a=1;
$tmpA=$findA;
$findB=$findA+$flagAL;
$findA=$findB;
while($a!=$b){
if(($findB = strpos($data, $flagB, $findB))!==false){
$b++;
if(($findA = strpos($data, $flagA, $findA))!==false){
if($findA>$findB){
if($a==$b){
//结束
$findB+=$flagBL;
$rn=substr($data,$tmpA,$findB-$tmpA);
} else {
$a++;
$findB=$findA+$flagAL;
$findA=$findB;
}
} else {
$a++;
$findA+=$flagAL;
$findB+=$flagBL;
}
} else {
if($a==$b){
//结束
$findB+=$flagBL;
$rn=substr($data,$tmpA,$findB-$tmpA);
} else {
//标记不完整
$findB+=$flagBL;
}
}
} else {
//标记不完整
$rn=substr($data,$tmpA);
$rn.=str_repeat($flagB,$a-$b);
break;
}
}
}
return $rn;
}
$html = file_get_contents('https://locate.apple.com/cn/zh/service/?pt=3&lat=23.134521&lon=113.358803');//获取源码
$find = strpos($html, 'window.resourceLocator.setup');
$json1 = dg_string($html, '{', '}', $find);//获取第一个JSON数据
$find = strpos($html, 'window.resourceLocator.storeSetup');
$json2 = dg_string($html, '{', '}', $find);//获取第二个JSON数据
$arr1 = json_decode($json1, true);//第一个JSON数据转为数组
$arr2 = json_decode($json2, true);//第二个JSON数据转为数组
print_r($arr1);
print_r($arr2);
//得到了数组,你想获取哪个参数都行了,你自己看着办吧,楼主可亲测代码
?>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-05-07
楼主试试这个
<html>
<head>

<meta http-equiv="content-type" content="text/html; charset=utf-8">

</head>
<?php
define ('REGEX_EXP','/ = ({.+}}});/');
$r =geturl('https://locate.apple.com/cn/zh/service/?pt=3&lat=23.134521&lon=113.358803');

preg_match(REGEX_EXP, $r, $found);

echo $found[1];

function geturl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, '');
curl_setopt($ch, CURLOPT_REFERER,'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$r = curl_exec($ch);
curl_close($ch);
return $r;
}
?>
</html>

弄好了就可以直接在网页上面用json操作了
第2个回答  2013-04-26
苹果使用的是https ,用file_get_contents()函数不行,要用curl的方式
<?php
$url = ("https://locate.apple.com/cn/zh/service/?pt=3&lat=23.134521&lon=113.358803");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
print_r($result);
?>
重点是以下两句:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);追问

非常谢谢你回答.抓取到全是乱码.还有我只需要下面这些信息怎么提取出呢?谢谢高手
北京直信创邺-广州天河区店2.4 km
广东省广州市天河区天河路490号壬丰大厦(西厅)17楼1703室
广州, 广东 510630
020-38889171

追答

乱码可能是你的php脚本编码问题,改为utf-8就应该没问题。
你需要的信息使用的是json格式,提取应该用正则表达式,有一定的复杂性,能搞定就是需要花时间和精力。

追问

还是不行.再试了一下就出错了"Google 已禁止对此应用使用地图 API。 此网站未获授权,无法使用您提供的 Google 地图客户端 ID。如果您是此应用的所有者,请访问以下网址详细了解如何注册网址:https://developers.google.com/maps/documentation/business/guide#URLs"
郁闷 还是抓不到.

追答

我这边是可以的啊,你右键浏览器查看一下源代码
window.resourceLocator.setup = 这部分json数据内容的就是你想要的

追问

一堆这样的东西.都抓不到想要的东西.郁闷啊!

追答

你的index.php文件不是utf-8格式的,而你所要获取的页面使用的是utf-8格式,这样就会出现乱码,最简单解决办法就是把你的index.php文件另存为utf-8格式,你可以用notepad+来打开php文件,然后点 格式-- 以utf-8无BOM格式编码,然后在保存,再刷新浏览器试试。

追问

是utf-8无BOM格式了.抓不到里面的数据.你试试随便抓个字符串看看能否抓到

追答

图片这里就是啦,图片看不到?

追问

看的到图片啊.我说抓不到里面的数据.你试试echo substr($result,6,5);截取部分数据.你试试能抓到不.

追答

要用正则来处理

追问

我知道要获取想要那些数据需要正则来提取.我现在测试随便抓取源码里面的字符都截取不到

追答

你要用 substr 你先把字符串中的空格去掉,然后别只截取一两个字符,汉字的特殊性你懂的

第3个回答  2013-05-04
简单做了一个,没有用正则,为这个定做的,直接切字符。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META content="text/html; charset=utf-8" http-equiv=Content-Type>
</HEAD>
<title>读取测试</title>
<BODY>
<?php

$r =geturl('https://locate.apple.com/cn/zh/service/?pt=3&lat=23.134521&lon=113.358803');
//echo "<PRE>".htmlspecialchars($r)."</PRE>\n";
$a=explode('"results":',$r);
$b=explode('"badges":',$a[1]);
$c=explode('":"',$b[0]);
$e='';
for($i=1;$i<count($c);$i++){
$d=explode('"',$c[$i]);
$e.=$d[0].' ';
}
$e=str_replace('DATA_FEED',"\r\n <br>",$e);
echo $e;

function geturl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, '');
curl_setopt($ch, CURLOPT_REFERER,'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$r = curl_exec($ch);
curl_close($ch);
return $r;
}
?>
</BODY>
</HTML>
相似回答