如何使用简单的dom抓取图像集合

时间:2019-04-27 08:45:45

标签: php web-scraping simple-html-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">

0 个答案:

没有答案