PHP从网站抓取数据


PHP crawling data from website

我目前正试图从一个网站抓取大量数据,但我正在努力与它一点点。它有一个a-z索引和一个1-20索引,所以里面有一堆循环和DOM的东西。然而,它在第一次运行时设法爬行并保存了大约10,000行,但现在我在大约15,000行,每次运行只爬行大约100行。

这可能是因为它必须跳过已经插入的行(对此进行了检查)。我想不出一个方法可以轻松地跳过一些页,因为1-20的索引变化很大(一个字母有18页,其他字母只有2页)。

我正在检查是否已经有给定ID的记录,如果没有,插入它。我认为这会很慢,所以现在在脚本开始运行之前,我检索所有行,然后使用in_array()进行检查,假设这样更快。但它就是行不通。

所以我的爬虫浏览26个字母,每个字母20页,然后每页多达50次,所以如果你计算一下,这是很多。

想过一个字母一个字母地运行,但这并不真正起作用,因为我仍然停留在"a"上,不能直接跳到"b"上,因为我会错过"a"上的记录。

希望我已经解释的问题足够好,有人来帮助我。我的代码看起来是这样的:(我已经删除了一些东西在这里和那里,猜测所有重要的东西都在这里给你一个想法)

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }
    return false;
}
/* CONNECT TO DB */
mysql_connect()......

$qry = mysql_query("SELECT uid FROM tableName");
$all = array();
while ($row = mysql_fetch_array($qru)) {
    $all[] = $row;
} // Retrieving all the current database rows to compare later
foreach (range("a", "z") as $key) {
    for ($i = 1; $i < 20; $i++) {
        $dom = new DomDocument();
        $dom->loadHTMLFile("http://www.crawleddomain.com/".$i."/".$key.".htm");
        $finder = new DomXPath($dom);
        $classname="table-striped";
        $nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
        foreach ($nodes as $node) {
            $rows = $finder->query("//a[contains(@href, '/value')]", $node);
            foreach ($rows as $row) {
                $url = $row->getAttribute("href");
                $dom2 = new DomDocument();
                $dom2->loadHTMLFile("http://www.crawleddomain.com".$url);
                $finder2 = new DomXPath($dom2);
                $classname2="table-striped";
                $nodes2 = $finder2->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname2 ')]");
                foreach ($nodes2 as $node2) {
                    $rows2 = $finder2->query("//a[contains(@href, '/loremipsum')]", $node2);
                    foreach ($rows2 as $row2) {
                        $dom3 = new DomDocument();
                        //
                        // not so important variable declarations..
                        //

                        $dom3->loadHTMLFile("http://www.crawleddomain.com".$url);
                        $finder3 = new DomXPath($dom3);
                        //2 $finder3->query() right here

                        $query231 = mysql_query("SELECT id FROM tableName WHERE uid='$uid'");
                        $result = mysql_fetch_assoc($query231);
                        //Doing this to get category ID from another table, to insert with this row..
                        $id = $result['id'];

                        if (!in_array_r($uid, $all)) { // if not exist
                            mysql_query("INSERT INTO')"); // insert the whole bunch
                        }
                    }
                }
            }
        }
    }
}

$uid没有定义,同样,这个查询没有意义:

mysql_query("INSERT INTO')");

你应该打开错误报告:

ini_set('display_errors',1); 
error_reporting(E_ALL);

在你的查询之后,你应该做一个or die(mysql_error());

而且,如果我不说,别人也会说。不要使用mysql_*函数。它们已被弃用,并将从PHP的未来版本中删除。PDO。