简单的HTML DOM PHP Web爬网程序不遵循初始爬网页面中的链接

时间:2013-10-21 01:22:01

标签: php web-crawler simple-html-dom

我正在使用Simple HTML DOM开发PHP网络抓取工具。这是我的代码:

<?php

include_once('simplehtmldom/simple_html_dom.php');


$seeds = [
    'http://www.google.com/?q=web+development#q=web+development',
    'http://www.google.com/?q=art#q=art'
];

// Web crawl
function crawl($seeds) {
    foreach($seeds as $key) {
        $html = new simple_html_dom();
        $html->load_file($key);
        foreach ($html->find('a') as $link) {
            array_push($seeds, $link->href);
        }
    }
    $seeds = array_unique($seeds);
    print_r($seeds);
}

?>

字符串simplehtmldom/simple_html_dom.php是Simple HTML DOM的路径。问题是它只会抓取$seeds数组中的最初2个网址(“http://www.google.com/?q=web+development#q=web+development”,“http://www.google.com/?q=art#q=art”)。但是,我希望它抓取第二个foreach循环推送到数组的所有URL。我该如何解决这个问题?

最后,处理增加的$seeds数组的最佳方法是什么?它会不停地爬行,所以我想跟踪所有的URL。我应该把它写到文件中,还是我最好的选择就是在这么久之后停止它(这是最好的方法是什么?)?我需要能够在服务器上运行的另一个PHP文件中或者并行地从同一个PHP文件中使用该数组。

0 个答案:

没有答案
相关问题