douqiao5552
2017-04-28 22:04
浏览 63
已采纳

如何在php中使用curl xpath在网站上获取特定图片

Through this code I can get the specific text but my problem is how I can get the specific picture on a website through this code. Can anyone help, I am stuck at this point.

<?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;

?>

图片转代码服务由CSDN问答提供 功能建议

通过这段代码我可以得到具体的文字,但我的问题是如何通过网站获取特定的图片 这段代码。 任何人都可以提供帮助,我现在陷入困境。

 &lt;?php 
 
ini_set('max_execution_time',300); 
 
功能新闻($ 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-&gt; loadHTML($ html  ); 
 libxml_clear_errors(); 
 $ xpath = new DOMXPath($ thispage); 
 $ status = $ xpath-&gt; evaluate($ path); 
返回$ 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; 
 
?&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanbi5906 2017-04-29 02:06
    已采纳

    Assuming you want the image the appears next to the first headline, the XPath is:

    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;
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题