![](/img/trans.png)
[英]How to scrape specific data from scrape with simple html dom parser
[英]How to scrape specific data from scrape with simple html dom parser
我正在嘗試從亞馬遜網頁上的產品中抓取價格數據,但我不僅獲得了變量中的價格數據,還獲得了包括<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英鎊。
我不確定是否在查找過程中執行此操作,或者不確定當我回顯變量時,是否指定所需內容?
干杯
只是使用
$result=$price_data1->innertext;
你一定會得到欲望的輸出。
更改XPath以選擇<b>
元素的text()
子元素,而不是選擇元素本身。
$price_data1 = $html1->find('b[class=priceLarge]/text()',0);
您可以嘗試在線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")); echo $data->price;
<b class="priceLarge">£163.00</b>
只需使用以下內容:
$p = "/b class=\"priceLarge\">(.*)<\/b>/";
preg_match($p, $html, $match)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.