通过这段代码,我可以得到具体的文字,但我的问题是如何通过这段代码在网站上获得特定的图片。任何人都可以帮忙,我现在陷入困境。
<?php
ini_set('max_execution_time', 300);
function news($url,$path){
$curl=curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$html=curl_exec($curl);
if(!empty($curl)){
$thispage= new DOMDocument;
libxml_use_internal_errors(true);
$thispage->loadHTML($html);
libxml_clear_errors();
$xpath=new DOMXPath($thispage);
$status=$xpath->evaluate($path);
return $status;
}
}
$a= news('https://www.dawn.com/latest-news','string(/html/body/div[2]/div/main/div/div/div[1]/article[1]/h2/a)');
echo $a;
?>
答案 0 :(得分:0)
假设您希望图像出现在第一个标题旁边,则XPath为:
function news($url,$path){
$curl=curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$html=curl_exec($curl);
if(!empty($curl)){
$thispage= new DOMDocument;
libxml_use_internal_errors(true);
$thispage->loadHTML($html);
libxml_clear_errors();
$xpath=new DOMXPath($thispage);
$status=$xpath->evaluate($path);
return $status;
}
}
$a= news('https://www.dawn.com/latest-news','string(/html/body/div[2]/div/main/div/div/div[1]/article[1]/figure/div/a/img/@src)');
echo $a;