2012-02-28 59 views
0

我正在创建一个将URL传递并获取页面内容的函数。如果此页面包含“下一步>”,我想抓取该网址并继续进入下一页下的页面,该页面不再包含下一页。需要帮助了解如何循环功能

这将如何完成?一个while循环?

check_url("http://site.com"); 
-> url contains 'next', href is http://site.com/ggkdoe 

-> does http://site.com/ggkdoe contain next? if so, hit it again and check if that contains 'next' then get that url etc etc 

明白吗?如何才能做到这一点?

预先感谢您

+1

到目前为止尝试过什么? – 2012-02-28 09:00:09

+0

通常,“下一步”按钮由服务器端生成,不解析客户端输出。 – Raptor 2012-02-28 09:00:57

+0

[强大的,成熟的HTML解析器的PHP]的可能重复(http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php) – CodeCaster 2012-02-28 09:01:00

回答

0

最有可能是这样的:

<?php 
$checkNext = false; 
$currentURL = "http://site.com"; 
do { 
    $check = check_url($currentURL); 
    if ($check !== null) { 
     $currentURL = $check; 
     $checkNext = true; 
    } else { 
     $checkNext = false; 
    } 
} while ($checkNext); 

而且我认为check_url()将返回一个URL,如果能找到和null否则。 do - while -loop确保至少对初始URL执行一次检查,然后再次检查,只要check_url()可以找到另一个URL。最后使用$currentURL为你想做的任何事情。

0

你可以使用递归性的完整链接的搜索:

function checkUrl($url) { 
    $atLeastOneUrl = true; 
    // Check your content 
    // Log some data about current Url 
    foreach ($urlFound in $urlsFound){ 
     check_url($urlFound); 
     $atLeastOneUrl=true; 
    } 

return $atLeastOneUrl; 
} 

但你会想看看这个链接1 - >连接2 - > - >链接1周期不会与地干扰您的搜索;)