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

如何在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;

?>
  • 写回答

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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler