通过PHP从网站提取数据 [英] Extract data from website via PHP
问题描述
我想为一些朋友创建一个简单的警报应用程序。
I am trying to create a simple alert app for some friends.
基本上我想从一个提取数据价格和库存可用性网址如下所示:
Basically i want to be able to extract data "price" and "stock availability" from a webpage like the folowing two:
- http://www.sparkfun.com/commerce/product_info.php?products_id=5
- http://www.sparkfun.com/commerce/product_info.php?products_id=9279
- http://www.sparkfun.com/commerce/product_info.php?products_id=5
- http://www.sparkfun.com/commerce/product_info.php?products_id=9279
我已经通过电子邮件和短信部分发出警报,但现在我想能够获得数量和价格网页(这2个或任何其他),以便我可以比较价格和数量可用,并提醒我们,如果一个产品在某些阈值之间订购。
I have made the alert via e-mail and sms part but now i want to be able to get the quantity and price out of the webpages (those 2 or any other ones) so that i can compare the price and quantity available and alert us to make an order if a product is between some thresholds.
已经尝试过一些regex(在一些教程中找到,但我的方式太n00b为此),但没有设法得到这个工作,任何好的提示或示例?
I have tried some regex (found on some tutorials, but i an way too n00b for this) but haven't managed to get this working, any good tips or examples?
推荐答案
$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";
这篇关于通过PHP从网站提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!