【发布时间】:2017-11-10 15:59:20
【问题描述】:
我正在尝试使用以下代码从 html 页面中捕获 div 的内容:
$content = file_get_contents('http://player.rockfm.fm/');
$content = preg_replace("/\r\n+|\r+|\n+|\t+/i", " ", $content);
preg_match('/<div id=\"metadata_player\">(.*?)<\/div>/', $content , $matchs);
print_r($matchs);
结果为空,因为该代码是由 javascript 或 ajax 生成的。 除了使用https://github.com/neorai/php-webdriver,还有其他方法吗?
解决方案:
$result = file_get_contents("http://bo.cope.webtv.flumotion.com/api/active?format=json&podId=78");
$array_full=(json_decode($result, true));
$symbols = array('"','}','{');
$array_full['value'] = str_replace($symbols, "", $array_full['value']);
$array_author_title= explode(",", $array_full['value']);
$array_author = explode(":", $array_author_title[1]);
$array_title = explode(":", $array_author_title[2]);
echo "Author: ".$array_author[1];
echo "</br>Title: ".$array_title[1];
感谢:@urban 和 How to use cURL to get jSON data and decode the data?
【问题讨论】:
-
为什么不使用 Javascript 捕获它!见phantomjs
-
扔掉这个
$content = preg_replace("/\r\n+|\r+|\n+|\t+/i", " ", $content);把这个'/<div id=\"metadata_player\">(.*?)<\/div>/'改成'/(?s)<div\s+id\s*=\s*"metadata_player"\s*>(.*?)<\/div\s*>/'