从网站上拖网搜索数据

时间:2016-11-29 16:39:55

标签: web-applications

将网站价格变为应用的最佳方法是什么?价格没有交付给我,因此我需要一种方法来自动搜索每天在应用程序中使用的产品及其网站价格。构建应用程序的软件尚未确定:)

1 个答案:

答案 0 :(得分:0)

我在php中使用curl从特定网页获取特定数据。 所以我的webapp在我的服务器上调用一个php脚本来从该特定网页获取数据。

以下代码利用e-pages.dk上的一个功能重定向到最新报纸,然后代码捕获最新报纸的编号并将该编号返回给调用的webapp(代码未显示)。

$urltopost = 'http://www.e-pages.dk/vesthimmerlandsavis/';
$ch = curl_init($urltopost);
curl_setopt($ch, CURLOPT_URL, $urltopost);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$log->write('URL: '.$urltopost, __FILE__, __LINE__);
$response = curl_exec($ch);
// Catch redirection
if (preg_match('#Location: (.*)#', $response, $matches))
{
    $redirect = trim($matches[1]);
    // $redirect = '/vesthimmerlandsavis/472'
    $log->write('Redirection: '.$redirect, __FILE__, __LINE__);
}
else
{
    die('Could not find newest newspaper');
}

// Catch number in /vesthimmerlandsavis/472
preg_match_all($pattern, $redirect, $matches);
$currentNumber = $matches[0][0];
$log->write('Current number: '.$currentNumber, __FILE__, __LINE__);