rosen_
rosen_

Reputation: 248

Group parser of a html page

the html page :

<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>

I wanna get http://url1.com, title, http://url.pdf, author.com and author if only class download has pdf url.

herre's the code :

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); 
            }
        }
    }
}

I got blank result, would you help me please, please show me the wrong. thanks in advance :)

Upvotes: 0

Views: 91

Answers (2)

Darian Lewin
Darian Lewin

Reputation: 182

Since the page is populated with div's with class title-download, you should be able to rewrite your loop as follows:

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); 
        }

    }

}

Upvotes: 1

ernie
ernie

Reputation: 6356

Have you tried adding print statements to try and debug? A quick look suggests that the third loop, where you have:

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

Isn't going to match on anything, as you're not going to go back far enough (looks like it'll be at the #ctl div?). Looks like you really want to be looking for a sibling element once you've gone up three levels?

Upvotes: 0

Related Questions