2011-12-07 33 views
0

我使用simpleHtmlDom来做一些基本的屏幕抓取。虽然我在抓住产品价格方面遇到了一些问题。有时我可以让它工作,有时候我不能。此外,有时我会得到多个价格......比如说,该网站有类似“通常100美元...现在79.99美元”的任何建议吗?目前,我使用的是这样的:php dom刮 - 抓取产品价格的最佳方法

$prices = array(); 
$prices[] = $html->find("[class*=price]", 0)->innertext; 
$prices[] = $html->find("[class*=msrp]", 0)->innertext; 
$prices[] = $html->find("[id*=price]", 0)->innertext; 
$prices[] = $html->find("[id*=msrp]", 0)->innertext; 
$prices[] = $html->find("[name*=price]", 0)->innertext; 
$prices[] = $html->find("[name*=msrp]", 0)->innertext; 

一个网站,我不知道该怎样从抢价格的想法是维多利亚的秘密....价格看起来它只是在随机HTML左右浮动。

+0

你有什么特别的问题吗?我们无法想出适合所有可能的标记的解决方案。看看http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php关于使用PHP解析HTML的一些提示。 – Gordon

+0

我期待看看人们用什么其他方法来获取产品价格以及获取正确的价格。我意识到这并不是一个“单一的解决方案”,但必须有比我目前所做的更好的事情。 – Stanley

回答

1

首先,不要使用simplehtmldom。使用内置的dom函数或基于它们的库。如果你想从页面中提取所有价格,你可以尝试这样的事情:

$html = "<html><body>normally $100... now $79.99</body></html>"; 
$dom = new DOMDocument(); 
$dom->loadHTML($html); 
$xpath = new DomXpath($dom); 

foreach($xpath->query('//text()[contains(.,"$")]') as $node){ 
    preg_match_all('/(\$[\d,.]+)/', $node->nodeValue, $m); 
    print_r($m); 
}