简单的Html DOM缓存

问题描述:

我正在使用简单的HTML DOM来刮取(有权限)一些网站。我基本上使用统计数据每天四次更新50个不同的网站。简单的Html DOM缓存

正如你可以想象的那样需要时间来做刮擦,因此我需要通过做一些缓存来加速进程。

我的愿景是:

DATA-PRESENTATION.php //所有的结果显示

SCRAPING.php //,使工作

我想建立一个代码以每天执行4次的方式在SCRAPING.PHP上执行cron作业,并将所有数据保存在caché中,然后将由DATA-PRESENTATION.PHP请求,从而为用户提供更快的体验。

我的问题是我该如何实现这个caché的东西?我是PHP的新手,我一直在阅读教程,但他们不是很有帮助,只有几个,所以我不能真正学会如何去做。

我知道其他的解决方案可能会实现一个数据库,但我不想这样做。另外,我一直在阅读关于memcached这样的高端解决方案,但该网站非常简单并且适合个人使用,所以我不需要那种东西。

谢谢!

SCRAPING.PHP

<?php 
include("simple_html_dom.php"); 

// Labour stats 
$html7 = file_get_html('http://www.website1.html'); 
$web_title = $html7->find(".title h1"); 
$web_figure = $html7->find(".figures h2"); 

?> 

DATA-PRESENTATION.PHP

<div class="news-pitch"> 
<h1>Webiste: <?php echo utf8_encode($web_title[0]->plaintext); ?></h1> 
<p>Unemployment rate: <?php echo utf8_encode($web_figure[0]->plaintext); ?></p> 
</div> 

最终代码!非常感谢@jerjer和@ PaulD.Waite,没有你的帮助,我无法真正做到这一点!

文件:

1- DataPresentation.php //这里我告诉请Cache.html

2- Scraping.php 数据//这里我凑了点,然后保存结果Cache.html

3- Cache.html //这里的刮结果保存

我在Scraping.php上设置了一个Cron作业,告诉它每次都覆盖Cache.html。

1- DataPresentation.php

<?php 
include("simple_html_dom.php"); 

$html = file_get_html("cache/test.html"); 
$title = $html->find("h1"); 
echo $title[0]->plaintext; 
?> 

2- Scraping.php

<?php 
include("simple_html_dom.php"); 

// by adding "->find("h1")" I speed up things as it only retrieves the information I'll be using and not the whole page. 
$filename = "cache/test.html"; 
$content = file_get_html ('http://www.website.com/')->find("h1"); 
file_put_contents($filename, $content); 
?> 

3-缓存。HTML

<h1>Current unemployment 7,2%</h1> 

它立即通过设置的东西这样我保证总有要加载一个缓存文件加载。

+0

您可以使用文件而不是数据库缓存 – jerjer 2011-12-15 09:03:15

这里是一个基于文件的缓存的样本:

<?php 
    // Labour stats 
    $filename = "cache/website1.html"; 
    if(!file_exists($filename)){ 
     $content = file_get_contents('http://www.website1.html'); 
     file_put_contents($filename, $content); 
    } 

    $html7 = file_get_html($filename); 
    $web_title = $html7->find(".title h1"); 
    $web_figure = $html7->find(".figures h2"); 

?> 
+1

你需要添加一些代码,使其刷新,每日4次,但。如果我正确理解了这些代码,它会刮掉网站一次,然后永久加载缓存的文件。例如,cron作业可以在每次运行时删除缓存文件。 – 2011-12-15 09:12:58

尝试使用Zend_Cache是​​从库Zend_Framework。这是很简单的使用方法:

function loadHtmlWithCache($webAddress){ 

    $frontendOptions = array(
     'lifetime' => 7200, // cache lifetime of 2 hours 
     'automatic_serialization' => true 
    ); 

    $backendOptions = array(
     'cache_dir' => './tmp/' // Directory where to put the cache files 
    ); 

    // getting a Zend_Cache_Core object 
    $cache = Zend_Cache::factory('Core', 
           'File', 
           $frontendOptions, 
           $backendOptions); 

    if(($result = $cache->load($webAddress)) === false) { 


     $html7 = file_get_html($webAddress); 
     $web_title = $html7->find(".title h1"); 
     $web_figure = $html7->find(".figures h2"); 
     $cache->save($webAddress,array('title'=>$web_title,'figure' => $web_figure)); 

    } else { 

     // cache hit! shout so that we know 
     $web_title = $result['title']; 
     $web_figure = $result['figure']; 

    } 

}