【问题标题】:Getting a product price from an ecommerce site using file get content使用文件获取内容从电子商务网站获取产品价格
【发布时间】:2021-07-01 06:12:33
【问题描述】:

我正在尝试使用 php 和 file_get_contents 函数获取产品价格。这是我的代码。它正在加载整个页面,但我只需要此页面的价格。有人可以帮忙吗?谢谢

<?php
$homepage = file_get_contents('https://www.snapdeal.com/product/bhawna-collection-loard-shiv-trishul/672311651336');
echo $homepage;
?>

【问题讨论】:

    标签: php file-get-contents regex-lookarounds


    【解决方案1】:

    PHP Simple HTML DOM parser这种东西真是天赐之物

    获取code here

    只需将其包含在您的 php 中

    require_once('./path/to/SimpleHtmlDom.php');
    // Create DOM from URL or file
    $html = file_get_html('https://www.snapdeal.com/product/bhawna-collection-loard-shiv-trishul/672311651336');
    
    // Find all images
    $price = $html->find('.pdp-final-price .payBlkBig', 0)->plaintext;
    

    更多关于this awesome tool

    【讨论】:

      【解决方案2】:

      使用标准函数

       <?php
          
          $homepage = file_get_contents('https://www.snapdeal.com/product/bhawna-collection-loard-shiv-trishul/672311651336');
          $search = '<span class="payBlkBig"  itemprop="price">';
          $subStrPos = strpos($homepage, $substring) + strlen($search);
          $subStr = substr($string, $subStrPos);
          $price = substr($subStr, 0, strpos($subs, '</span>'));
          
          echo $price;
      

      【讨论】:

      • 根据strpos()的文档:Warning: This function may return Boolean false, but may also return a non-Boolean value which evaluates to false.
      猜你喜欢
      • 2017-07-20
      • 2021-01-07
      • 1970-01-01
      • 2018-03-15
      • 1970-01-01
      • 2015-09-22
      • 1970-01-01
      • 2011-10-03
      • 1970-01-01
      相关资源
      最近更新 更多