Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用xpath和php提取html_Php_Html_Xpath_Goutte - Fatal编程技术网

使用xpath和php提取html

使用xpath和php提取html,php,html,xpath,goutte,Php,Html,Xpath,Goutte,我似乎找不到如何使用xpath获取html页面的值。我试图在一个页面上检索每个产品的图像来源、价格和名称……我尽可能地检索产品的数量,但之后不知何故无法获取任何值……我肯定不是专业人士,这可以解释;) 我试了一些东西。我可以在Chrome中看到xpath并尝试使用它们,但它总是空的。在这一点上,我不知道该尝试什么 $8.00 函数url\u获取内容($url){ 如果(!function_存在('curl_init')){ 模具('未安装卷曲!'); } $ch=curl_init(); c

我似乎找不到如何使用xpath获取html页面的值。我试图在一个页面上检索每个产品的图像来源、价格和名称……我尽可能地检索产品的数量,但之后不知何故无法获取任何值……我肯定不是专业人士,这可以解释;)

我试了一些东西。我可以在Chrome中看到xpath并尝试使用它们,但它总是空的。在这一点上,我不知道该尝试什么


$8.00
函数url\u获取内容($url){
如果(!function_存在('curl_init')){
模具('未安装卷曲!');
}
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$output=curl\u exec($ch);
卷曲关闭($ch);
返回$output;
}
$newDom=newdomdocument;
$html=url\u get\u contents('test.html');
$newDom->loadHTML($html);
$newDom->preserveWhiteSpace=false;
$finder=newdomxpath($newDom);
$products=$finder->query('//div[@class=“prod main”]');
foreach($products as$product){
$img=$finder->query('/div[2]/div/a/img/@src',$clip)[0]->value;
}

phparray(24){[0]=>NULL[1]=>NULL[2]=>NULL[3]=>NULL[4]=>NULL[5]=>NULL[7]=>NULL[8]=>NULL[9]=>NULL[10]=>NULL[11]=>NULL[12]=>NULL[13]=>NULL[14]=>NULL[15]=>NULL[16]=>NULL[17]=>NULL[18]=>NULL[19]=>NULL[20]=>NULL[21]=>NULL[22]=>NULL[23]>

好的,我用Goutter就到了

require 'vendor/autoload.php';
use Goutte\Client;
$url = "test.html";
$client = new Client();
$crawler = $client->request('GET', $url);
$title_array = array();
$titles=$crawler->filter('.prod-title')->each(function ($node){
    $title = $node->text();
    $title_array[]=$title;
    print_r($title_array);
});    
return $title_array;
现在的问题是print_r($title_数组)返回值,但$title_数组总是空的,我不明白为什么:/