【问题标题】:How to parse article page links in rails with pismo and mechanic?如何使用 pismo 和 mechanic 解析 Rails 中的文章页面链接?
【发布时间】:2013-01-17 11:47:22
【问题描述】:

我正在尝试解析多页文章上的链接以自动点击它们以提取整篇文章内容。我正在使用机械化,关于我的last questionhelpful answer

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

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>

ARSTECHNICA:

<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">→</span></span></a>

IGN:

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

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

希望你能帮帮我。

【问题讨论】:

    标签: ruby-on-rails html-parsing


    【解决方案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 解析 Url

    $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 网址

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多