简单的Html DOM缓存


Simple Html DOM Caching

我使用简单的HTML DOM抓取(经过许可)一些网站。我基本上收集了大约50个不同的网站的统计数据,这些数据大约每天更新四次。

正如你所想象的那样,做抓取需要花费时间,因此我需要通过做一些缓存来加速这个过程。

我的愿景是:

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

scraper .php//生成作业的代码

我想在SCRAPING.PHP上设置一个cron作业,它每天执行4次,并将所有数据保存在缓存中,然后由data - presentation . php请求,使用户的体验更快。

我的问题是我如何实现这个缓存的东西?我是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和@ paul。怀特,没有你的帮助我真的做不到!

文件:

1- datpresentation .php //这里我显示请求给Cache.html的数据

2- scrap.php //这里我抓取站点,然后将结果保存到Cache.html

3- Cache.html //这里保存了抓取结果

我在scraper .php上设置了一个Cron Job,告诉它每次覆盖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 - Cache.html

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

它立即加载,通过这样设置,我确保总是有一个缓存文件要加载

下面是一个基于文件的缓存示例:

<?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");
?>

尝试使用Zend_Framework中的Zend_Cache库。使用起来很简单:

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'];
    }

}