【问题标题】:Group parser of a html pagehtml页面的组解析器
【发布时间】:2012-09-20 20:27:50
【问题描述】:

html 页面:

<div class="title-download">
    <div id="ctl " class="title">
        <h3>
            <a id="ct2" href="http://url1.com">title</a>
            <span id="ct3" class="citation">(<a id="ct4 " href=" ">Citations</a>)</span></h3>
    </div>
    <div id="ct4" class="download">
        <a id="ct5 " title=" " href="http://url.pdf" img id="ct6" class="small-icon" src=" " /></a>
    </div>
</div>
<div class="content">
    <a class="author " href="author.com">author</a><span class="span-break" >, </span><a class="author2.com " href="http://author2.com">author2</a>
</div>

我想得到http://url1.comtitlehttp://url.pdfauthor.comauthor,如果只有类下载有 pdf 网址。

这是代码:

foreach($html->find('span[class=citation]') as $link1){
    foreach($link1->parent()->parent()->parent()->find('.download a') as $link2){
        foreach ($link1->parent()->find('div[class=content] a') as $a ){
            if(strtolower(substr($link2->title, strrpos($link2->href, '.'))) === '.pdf') {
                $link1 = $link1->prev_sibling();
                $a = $link1->next_sibling();
                $title = strip_tags($link1->plaintext);
                $linkWeb = strip_tags($link1->href);
                $author= strip_tags($a->plaintext);
                $linkAuthor= strip_tags($a->href); 
                $pdfLink = strip_tags($link2->title); 
            }
        }
    }
}

我得到了空白结果,请你帮我,请告诉我错误。在此先感谢:)

【问题讨论】:

  • @WebnetMobile.com 调用$html = file_get_html('http://www.google.com/');simplehtmldom.sourceforge.net的结果
  • 所以更新你的问题。
  • 文件是否填充了不同的 div 类 title-download?
  • @DarianLewin 是的,这是不同的 div 类标题下载
  • @WebnetMobile.com 这不是我的问题

标签: php html parsing dom simple-html-dom


【解决方案1】:

由于页面填充了具有类 title-download 的 div,您应该能够按如下方式重写循环:

foreach( $html->find('div[class=title-download]') as $div){
    $dowloadlink = $div->find('div[class=download] a', 0);

    if($dowloadlink != null){

        if(strtolower(substr($downloadlink->href, strrpos($downloadlink->href, '.'))) === '.pdf'){
            $content = $div->find('div[class=content] h3 a', 0);

            $title = strip_tags($content->plaintext);
            $linkWeb = strip_tags($content->href);

            $authorlink = $div->next_sibling().find('a', 0);
            $author = strip_tags($authorlink->plaintext);
            $linkAuthor= strip_tags($authorlink->href);

            $pdfLink = strip_tags($downloadlink->href); 
        }

    }

}

【讨论】:

  • 对不起,我注意到一个小错误,我仍在使用 $link2 搜索 pdf 文件名中的点,但我从 $downloadlink 获取链接,我忘了放一个)在foreach中,我将编辑我的帖子
【解决方案2】:

您是否尝试过添加打印语句来尝试调试?快速浏览一下,您可以看到第三个循环:

foreach ($link1->parent()->parent()->find('div[class=content] a') as $a) {

不会匹配任何内容,因为您不会回溯到足够远的地方(看起来它会在#ctl div 处?)。看起来你真的想在升三级后寻找兄弟元素?

【讨论】:

  • 我试图打印结果,是的,先生。我很困惑在第三个循环中获取作者元素。如果我与其他人组队,如何获得他们?
  • 作者位于一个div中,该div是title-download类的div的下一个兄弟,所以可以使用$link1->parent()->next_sibling().find(' a', 0);如果要将它们组合在一起,这将取决于您是使用代码并从 $link1 开始搜索,还是使用首先搜索周围 div 的 my 方法。
猜你喜欢
  • 2012-01-06
  • 1970-01-01
  • 1970-01-01
  • 2011-10-09
  • 1970-01-01
  • 2012-08-19
  • 2011-07-30
  • 2013-02-28
  • 1970-01-01
相关资源
最近更新 更多