我从http://sports.in.msn.com/football-world-cup-2014/south-africa-v-brazil/1597383
解析分数我能够解析所有属性。但我无法解析时间。
我用过
$homepages = file_get_html("http://sports.in.msn.com/football-world-cup-2014/south-africa-v-brazil/1597383");
$teama = $homepages->find('span[id="clock"]');
请帮助我
答案 0 :(得分:1)
由于该特定站点正在动态加载值(通过AJAX请求),因此您无法在初始加载时真正解析该值。
<span id="clock"></span> // this tends to be empty initial load
正常报废:
$homepages = file_get_contents("http://sports.in.msn.com/football-world-cup-2014/south-africa-v-brazil/1597383");
$doc = new DOMDocument();
@$doc->loadHTML($homepages);
$xpath = new DOMXPath($doc);
$query = $xpath->query("//span[@id='clock']");
foreach($query as $value) {
echo $value->nodeValue; // the traversal is correct, but this will be empty
}
我的建议不是抓它,你还需要通过请求来访问它,因为这是一个时间(当然,随着比赛的进行,这将改变并改变直到比赛结束)。或者您也可以使用他们的请求。
$url = 'http://sports.in.msn.com/liveplayajax/SOCCERMATCH/match/gsm/en-in/1597383';
$contents = file_get_contents($url);
$data = json_decode($contents, true);
echo '<pre>';
print_r($data);
echo '</pre>';
应该产生类似的东西(实际上是它的一部分):
[2] => Array
(
[Code] =>
[CommentId] => -1119368663
[CommentType] => manual
[Description] => FULL-TIME: South Africa 0-5 Brazil.
[Min] => 90'
[MinExtra] => (+3)
[View] =>
[ViewHint] =>
[ViewIndex] => 0
[EditKey] =>
[TrackingValues] =>
[AopValue] =>
)
您应该使用foreach获取90'
。考虑这个例子:
foreach($data['Commentary']['CommentaryItems'] as $key => $value) {
if(stripos($value['Description'], 'FULL-TIME') !== false) {
echo $value['Min'];
break;
}
}
应打印:90'