我如何在 php 中使用 curl xpath 在网站上获取特定图片
How I can get specific picture on a website using curl xpath in php
通过这段代码我可以得到具体的文字,但我的问题是如何通过这段代码得到网站上的具体图片。谁能帮忙,我卡在这了。
<?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;
?>
假设您希望图片出现在第一个标题旁边,则 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;
通过这段代码我可以得到具体的文字,但我的问题是如何通过这段代码得到网站上的具体图片。谁能帮忙,我卡在这了。
<?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;
?>
假设您希望图片出现在第一个标题旁边,则 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;