0

我正在尝试解析多页文章上的链接以自动单击它们以提取整篇文章内容。关于我的最后一个问题有用的答案,我正在使用机械化。

如何搜索分页链接?每篇文章可能有不同的链接架构,例如:

ZEITONLINE:

<a id="hp.article.bottom.paginierung.2" class="pn-forward pn-button" title="Vor" href="http://www.zeit.de/politik/ausland/2013-01/Syrien-Fotografie-Reportage/seite-2">Vorwärts</a>

技术:

<a href="http://arstechnica.com/information-technology/2013/01/help-ive-got-windows-8-and-i-miss-my-start-menu/2"><span class="next">Next <span class="arrow">→&lt;/span></span></a>

IGN:

<a href="http://www.ign.com/articles/2013/01/03/the-ultimate-2013-movie-preview?page=2">Next&nbsp;»</a>

对于 IGN 链接,解析链接相对简单,因为它包含链接文本Next。但是其他链接呢?我知道它应该是可行的,因为口袋、可读性和 instapaper 正在提取多页内容。

希望你能帮助我一点。

4

1 回答 1

1

我为此写了一个函数

function proccessURL($ParentURL,$URL){
   $parse_url=parse_url($URL);
    if(@$parse_url['host']==""){
        $Parent_URL=parse_url($ParentURL);
        $path=explode("/",@$parse_url['path']);
        $redirect=0;    
        $lkey=0;
        $flag=false;
        while(list($key,$val)=each($path)){
            if($val==".." or $val=="." or $val=="..."){
                $redirect++;
                $lkey=$key;
                $flag=true;
            }else{
                break;
            }
        }
        if($flag){
           $matches=explode("/",$Parent_URL['path']);
           end($matches);
           $b=each($matches);
           $n=$b['key'];
           $url='';
           for($i=0;$i<$n-$redirect;$i++){
               $url.=$matches[$i]."/";
           }   
           for($i=$redirect+1;next($path);$i++){
               $url.=$path[$i]."/";
           }
           rtrim($url,"/");
           $parse_url['path']=$url;
        }else{
            $parse_url['path']="/".@$parse_url['path'];
        }
    }else{
        $Parent_URL['scheme']=$parse_url['scheme'];
        $Parent_URL['host']=$parse_url['host'];
    }
    //print_r($parse_url);
    if(@$parse_url['query']!=""){
        $parse_url['query']="?".@$parse_url['query'];
    }
    if(@$parse_url['fragment']!=""){
        $parse_url['fragment']="#".@$parse_url['fragment'];
    }
    return $Parent_URL['scheme']."://".@$Parent_URL['host'].@$parse_url['path'].@$parse_url['query'].@$parse_url['fragment'];
}

此函数解决链接地址

样本:

$CorrectLink=proccessURL("http://www.sepidarcms.ir/kernel/","../plugin/1.php");

输出为“http://www.sepidarcms.ir/plugin/1.php” 现在您可以通过 preg_match_all 解析网址

$html="Your HTML Str";
$URL="Your HTML Page Link";
preg_match_all("/href=\"([^\"]*)\"/is", $html, $matches);
while(list($key,$val)=each($matches[1])){
$val=proccessURL($URL,$val);
    echo $val;
}

此代码为您正确列出所有href Url

于 2013-01-17T12:10:34.707 回答