Mik*_*ike 8 php regex curl html-parsing
我正在尝试为一些朋友创建一个简单的警报应用程序.
基本上我希望能够从如下两个网页中提取数据"价格"和"库存可用性":
我已通过电子邮件和短信部分发出警报但现在我希望能够从网页(那些2或任何其他网页)中获取数量和价格,以便我可以比较可用的价格和数量并提醒我们如果产品介于某个阈值之间,则进行订单.
我已经尝试了一些正则表达式(在一些教程中找到,但我的方式太过于n00b)但是还没有设法让这个工作,任何好的技巧或例子?
Mat*_*iva 32
$content = file_get_contents('http://www.sparkfun.com/commerce/product_info.php?products_id=9279');
preg_match('#<tr><th>(.*)</th> <td><b>price</b></td></tr>#', $content, $match);
$price = $match[1];
preg_match('#<input type="hidden" name="quantity_on_hand" value="(.*?)">#', $content, $match);
$in_stock = $match[1];
echo "Price: $price - Availability: $in_stock\n";
它被称为屏幕抓取,以防你需要谷歌搜索它.
我建议您使用dom解析器和xpath表达式.首先通过HtmlTidy提供HTML,以确保它是有效的标记.
例如:
$html = file_get_contents("http://www.example.com");
$html = tidy_repair_string($html);
$doc = new DomDocument();
$doc->loadHtml($html);
$xpath = new DomXPath($doc);
// Now query the document:
foreach ($xpath->query('//table[@class="pricing"]/th') as $node) {
  echo $node, "\n";
}
| 归档时间: | 
 | 
| 查看次数: | 56384 次 | 
| 最近记录: |