【问题标题】:How to scrape specific data from scrape with simple html dom parser如何使用简单的 html dom 解析器从抓取中抓取特定数据
【发布时间】:2011-08-02 03:57:56
【问题描述】:

我试图从亚马逊网页上的产品中抓取价格数据,但我获得的不仅仅是变量中的价格数据,我还获得了其他元素,例如 <span> 等。代码...

include 'simple_html_dom.php';
$html1 = file_get_html('http://www.amazon.co.uk/New-Apple-iPod-touch-Generation/dp/B0040GIZTI/ref=br_lf_m_1000333483_1_1_img?ie=UTF8&s=electronics&pf_rd_p=229345967&pf_rd_s=center-3&pf_rd_t=1401&pf_rd_i=1000333483&pf_rd_m=A3P5ROKL5A1OLE&pf_rd_r=1ZW9HJW2KN2C2MTRJH60');

$price_data1 = $html1->find('b[class=priceLarge]',0);

该变量还包含<b class="priceLarge">£163.00</b>等数据

有没有办法去除不需要的数据?我只需要 163.00 英镑。

我不确定是否在查找过程中执行此操作,或者当我回显变量时,是否指定我想要的内容?

干杯

【问题讨论】:

    标签: php html parsing dom


    【解决方案1】:
    <b class="priceLarge">£163.00</b>
    

    只需使用以下内容:

    $p = "/b class=\"priceLarge\">(.*)<\/b>/";
    preg_match($p, $html, $match)
    

    【讨论】:

      【解决方案2】:

      只需使用

      $result=$price_data1->innertext;
      

      你一定会得到欲望的输出。

      【讨论】:

        【解决方案3】:

        您可以尝试在线 API,例如 Synthetics Web。您可以用最少的编码工作来提取数据。

        $url = urlencode('http://www.amazon.co.uk/New-Apple-iPod-touch-Generation/dp/B0040GIZTI/ref=br_lf_m_1000333483_1_1_img?ie=UTF8&s=electronics&pf_rd_p=229345967&pf_rd_s=center-3&pf_rd_t=1401&pf_rd_i= 1000333483&pf_rd_m=A3P5ROKL5A1OLE&pf_rd_r=1ZW9HJW2KN2C2MTRJH60'); $wid = '160'; $data = json_decode(file_get_contents("http://www.syntheticsweb.com/resources/www.json?wid=$wid&url=$url")); 回声$数据->价格;

        【讨论】:

        • 到目前为止,您的所有答案似乎都推荐 Synthetics Web。你有任何附属关系吗?如果是这样,您必须在回答中明确说明您的从属关系,如常见问题解答中所述。
        【解决方案4】:

        更改您的 XPath 以选择 &lt;b&gt; 元素的 text() 子元素,而不是选择元素本身。

        $price_data1 = $html1->find('b[class=priceLarge]/text()',0);
        

        【讨论】:

          猜你喜欢
          • 2015-01-07
          • 1970-01-01
          • 2016-02-22
          • 2015-07-25
          • 1970-01-01
          • 2021-05-18
          • 2011-09-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多