如何使用简单的html dom解析器从scrape中抓取特定数据

时间:2011-04-09 17:09:20

标签: php html parsing dom

我正在尝试从亚马逊网页上的产品中获取价格数据,但我得到的不仅仅是变量中的价格数据,还包括其他元素,例如<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英镑。

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

干杯

4 个答案:

答案 0 :(得分:1)

更改XPath以选择text()元素的<b>子元素,而不是选择元素本身。

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

答案 1 :(得分:1)

只需使用

$result=$price_data1->innertext;

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

答案 2 :(得分:0)

您可以尝试使用Synthetics Web等在线API。您可以用最少的编码工作量提取数据。

$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;

答案 3 :(得分:-1)

<b class="priceLarge">£163.00</b>

只需使用以下内容:

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