0

希望这是一个非常简单的解决方案,我是 PHP 新手,所以我可能遗漏了一些明显的东西。我正在用 ScraperWiki 构建一个刮板(尽管这是 PHP 的一个问题,与 SW 几乎没有关系)。代码如下:

<?php
require 'scraperwiki/simple_html_dom.php';

$allLinks = array();

function nextPage($nextUrl, $y)
{
    getLinks($nextUrl, $y);    
}

function getLinks($url) // gets links from product list page   
{
    global $allLinks;
    $html_content = scraperwiki::scrape($url);
    $html         = str_get_html($html_content);

    if (isset($y)) {
        $x = $y;
    } else {
        $x = 0;
    }

    foreach ($html->find("div.views-row a.imagecache-product_list") as $el) {
        $url          = $el->href . "\n";
        $allLinks[$x] = 'http://www.foo.com';
        $allLinks[$x] .= $url;
        $x++;
    }

    $next = $html->find("li.pager-next a", 0)->href . "\n";
    print_r("Printing $next:");
    print_r($next);

    if (isset($next)) {
        $nextUrl = 'http://www.foo.com';
        $nextUrl .= $next;
        print_r($nextUrl);
        $y = $x;
        print_r("Printing X:");
        print_r($x);
        print_r("Printing Y:");
        print_r($y);

        nextPage($nextUrl, $y);
    } else {
        return;
    }

}

getLinks("http://www.foo.com/department/accessories");

print_r($allLinks);

?>

预期输出:脚本应该从第一页抓取所有链接,找到“下一页”按钮,从其 URL 中抓取链接,从该 URL 中找到“下一页”等等。当没有更多“下一页”链接时,它应该停止。

CURRENT OUTPUT:代码运行良好,但它不会在应该停止的时候停止。这是关键行:

$next = $html->find("li.pager-next a", 0)->href . "\n";
if (isset($next)) { }

如果页面上存在a,我只希望“nextPage()”函数运行li.pager-next a。这是控制台的输出:

     http://www.foo.com/department/accessories?page=1
        http://www.foo.com/department/accessories?page=2
        http://www.foo.com/department/accessories?page=3
        http://www.foo.com/department/accessories?page=4
        http://www.foo.com/department/accessories?page=5
        http://www.foo.com/department/accessories?page=6
        http://www.foo.com/department/accessories?page=7
        http://www.foo.com/department/accessories?page=8
        http://www.foo.com/department/accessories?page=9
        http://www.foo.com/department/accessories?page=10

    PHP Notice:  Trying to get property of non-object in /home/scriptrunner/script.php on line 31
 // THE LOOP SHOULD BREAK HERE BUT DOESN'T

        http://www.foo.com
        http://www.foo.com/home?page=1
        http://www.foo.com/home?page=2
        http://www.foo.com/home?page=3
        http://www.foo.com/home?page=4
        http://www.foo.com/home?page=5
        http://www.foo.com/home?page=6
        http://www.foo.com/home?page=7
4

3 回答 3

1

那这个呢:

$next = $html->find("li.pager-next a", 0);

if (isset($next)) {
    $nextUrl = 'http://www.foo.com';
    $nextUrl .= $next->href; // move ->href here
    print_r($nextUrl . "\n"); // put \n here since we don't actually want that char in the url
    $y = $x;
    print_r("Printing X:");
    print_r($x);
    print_r("Printing Y:");
    print_r($y);

    nextPage($nextUrl, $y);
} else {
    return;
}
于 2013-02-22T23:58:06.283 回答
0

无论返回什么值

$next = $html->find("li.pager-next a", 0)->href . "\n";

当您附加到它时,它永远不会导致isset($next)返回 false "\n"

使用这样的东西:

$nextElement = $html->find("li.pager-next a", 0);

if(isset($nextElement))
{
    $nextUrl = 'http://www.foo.com' . $nextElement->href . PHP_EOL;

    print_r($nextUrl);
    $y = $x;
    print_r("Printing X:");
    print_r($x);
    print_r("Printing Y:");
    print_r($y);

    nextPage($nextUrl, $y);
}
于 2013-02-22T23:58:28.027 回答
-2

只需删除 isset()

    如果($下一个){
    }
    

于 2013-02-23T00:01:52.977 回答