【问题标题】:Collecting specific data from a website using cURL(php)使用 cURL(php) 从网站收集特定数据
【发布时间】:2014-04-19 15:44:01
【问题描述】:

我正在开发一个网站,其中一部分允许用户评论产品。我希望实现一个简单的目标,即列出产品并说“还没有评论!”

由于总是在制造产品,我想自动收集名称、价格,也许还有一张图片...

我目前正在尝试从HERE 访问此信息 这是我当前的代码:

<?php

$ch = curl_init("http://www.nrs.com/category/2740/whitewater-kayaking/womens-life-    jackets");
$fp = fopen("collected.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

它将数据收集到一个文本文件中。 以下是尝试解析 html 时的几个问题。 - 首先,我需要确保所有分页都已满,或者循环浏览页面。 - 我需要收集产品名称等...所以,删除某些代码 直到我只剩下我想要的信息 - 让它写出一个分隔符,如 |*|因此,当我的网站显示名称时,它就知道何时写出下一个产品。 - 让网站所有者难以阻止它......我希望他保持 div 名称等......相同。 - 一旦找不到就停止搜索

这是我认为在他们的网站上很重要的代码:

<div class="categoryItem">
    <div class="shortDesc">
    <p>The beautifully designed Astral Linda women's life jacket is affordable, lightweight and all-day comfortable for any type of boating. The thin back works comfortably with any kayak or raft seat.</p>
    </div>
    <a href="/product/40086.02/astral-womens-linda-pfd" data-prodImg="40086.02"><img   class="productImageThumb"   src="http://nrsweb5.richfx.com.edgesuite.net/image/media/40086_02_Azul_Front_010313_150x150.jpg" width="150" height="150" ALT="Astral Women&#39;s Linda PFD" /></a>
        <div class="productColorOptions">
        <a href="javascript:void(0);" title="Azul"><img src="http://nrsweb5.richfx.com.edgesuite.net/image/media/40086_02_Azul_Front_010313_swatch_15x15.jpg" alt="" border="0"></a>
        <a href="javascript:void(0);" title="Gray"><img  src="http://nrsweb5.richfx.com.edgesuite.net/image/media/40086_02_Gray_Front_010313_swatch_ 15x15.jpg" alt="" border="0"></a>
   </div>
   <div class="clearIt"></div>
   <h2><a href="/product/40086.02/astral-womens-linda-pfd">Astral Women's Linda PFD</a> </h2>
   <p class="reviewLinkBlock"></p>
   <h4>$94.95</h4>
   <div class="compareButton"><a href="javascript:void(0);" data-compare="40086.02"   rel="nofollow" class="compareBtn compareAdd">Compare</a><span class="cancelCompare"><a    href="javascript:void(0);" data-compare="40086.02" rel="nofollow"    class="compareRemove">x</a></span></div>
</div><!-- end class="categoryItem" -->

有多个 categoryItem div,并且仅专门用于产品。用户甚至很好地评论了它。所以现在我需要不断剥离代码层,直到我能找到名字......有什么建议/解决方案吗?

【问题讨论】:

    标签: php html parsing curl


    【解决方案1】:

    我建议使用正则表达式来匹配包含您需要的信息的元素,使用 preg_match_all。

    例如:

    preg_match_all('#\<p>(.+?)\<\/p>#s', $html, $matches);
    

    显然您可能想要修改模式以满足您的需要

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-22
      • 2014-03-08
      • 2019-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多