首页 > 解决方案 > 我如何使用简单的 dom 抓取图像集合

问题描述

我正在尝试抓取 Flipkart 产品的图像,但我得到的是占位符图像,而不是实际的产品图像。我也显示图像标签中的 alt 标签。alt 属性显示正确的 alt 标签值,但未获取正确的图像来源

'''

      require 'simple_html_dom.php';

      $prduct_name = $_GET['prod'];

      $url = "https://www.flipkart.com/search?q='".$prduct_name."'&sid=tyy%2C4io&as=on&as-show=on&otracker=AS_QueryStore_HistoryAutoSuggest_0_0&otracker1=AS_QueryStore_HistoryAutoSuggest_0_0&as-pos=0&as-type=HISTORY";

      $html = file_get_html($url);


      foreach ($html->find('div._3O0U0u') as $div) {
        $title = $div->find('div._3wU53n', 0)->plaintext;
        $price = $div->find('div._2rQ-NK', 0)->plaintext;
        $imgscr = $div->find('img._1Nyybr', 0)->src;
        $alt = $div->find('img._1Nyybr', 0)->alt;
        $link = $div->find('a._31qSD5', 0)->href;

        ?>   
      <div class="col-lg-4">
            <div class="card card-lift--hover shadow border-0">
              <div class="card-body py-5">
                <img src="<?php echo $imgscr; ?>" class="mx-auto d-block" alt="<?php echo $alt; ?>" height="200" width="100">
                  <br> 
                <h6 class="text-primary text-uppercase text-center"><?php echo $title; ?></h6>
              <div>
                  <span class="badge badge-pill badge-primary">Price : <?php echo $price; ?> </span>
              </div>
                <div class="row">
                  <div class="col-md-6">
                      <a href="compare.php?prod=<?php echo $title; ?>&url=https://www.flipkart.com<?php echo $link;?>" class="btn btn-sm btn-primary mt-4">Compare</a>
                  </div>
                  <div class="col-md-6">
                      <a href="https://www.flipkart.com<?php echo $link;?>" target="_blank" class="btn btn-sm btn-primary mt-4">View Product</a>
                  </div>
                </div>

              </div>
            </div>
       </div>
     <?php } ?>

'''

我想要实际值 src 属性

<img class="_1Nyybr Yun65Y _30XEf0" alt="Redmi Note 7 (Onyx Black, 64 GB)" src="https://rukminim1.flixcart.com/image/832/832/jskofww0/mobile/e/k/z/mi-redmi-note-7-na-original-imafe48yggp2za54.jpeg?q=70">

喜欢图像的链接,但它给了我

<img class="_1Nyybr Yun65Y _30XEf0" alt="Redmi Note 7 (Onyx Black, 64 GB)" src="//img1a.flixcart.com/www/linchpin/fk-cp-zion/img/placeholder_9951d0.svg">

标签: phpweb-scrapingsimple-html-dom

解决方案


推荐阅读