duanchi4544 2015-10-20 06:38
浏览 78

使用dom从谷歌中获取图像

I want to fetch image from google using PHP. so I tried to get help from net I got a script as I needed but it is showing this fatal error

Fatal error: Call to a member function find() on a non-object in C:\wamp\www q\qimages.php on line 7**

Here is my script:

<?php
include "simple_html_dom.php";
$search_query = "car";
$search_query = urlencode( $search_query );
$html = file_get_html( "https://www.google.com/search?q=$search_query&tbm=isch" );
$image_container = $html->find('div#rcnt', 0);
$images = $image_container->find('img');
$image_count = 10; //Enter the amount of images to be shown
$i = 0;
foreach($images as $image){
    if($i == $image_count) break;
    $i++;
    // DO with the image whatever you want here (the image element is '$image'):
    echo $image;
}
?>

I am also using Simple html dom.

  • 写回答

2条回答 默认 最新

  • douwen1313 2015-10-20 06:42
    关注

    Look at my example that works and gets first image from google results:

    <?php
    
    $url = "https://www.google.hr/search?q=aaaa&biw=1517&bih=714&source=lnms&tbm=isch&sa=X&ved=0CAYQ_AUoAWoVChMIyKnjyrjQyAIVylwaCh06nAIE&dpr=0.9";
    $content = file_get_contents($url);
    
    libxml_use_internal_errors(true);
    $dom = new DOMDocument;
    @$dom->loadHTML($content);
    $images_dom = $dom->getElementsByTagName('img');
    foreach ($images_dom as $img) {
        if($img->hasAttribute('src')){
            $image_url = $img->getAttribute('src');
        }
        break;
    }
    
    
    //this is first image on url
    echo $image_url;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器