-1

このhtmlページからタイトルとリンクを取得したい:

<div class="gs_r">
    <h3 class="gs_rt">
        <span class="gs_ctc">[BOOK]</span>
        <a href="http://example.com" onmousedown="return scife_clk(this.href,'','res','1')">titleA</a>
    </h3>
    <div class="gs_ggs gs_fl">
        <a href="http://exampleA.pdf" onmousedown="return scife_clk(this.href,'gga','gga','1')">

どうすれば入手できますか?

コードは次のとおりです。

<?php
include 'simple_html_dom.php';
$url = 'http://example.com';
$html = file_get_html($url);
//get the first link
foreach($html->find('span[class=gs_ctc]')as $Link){
echo $link;
}
foreach($html->find('div[class=gs_ggs gs_fl]')as $docLink){
echo $docLink;
}

?>
4

1 に答える 1

1

最初のリンクは、 の兄弟です<span>。これを試して:

//get the first link
foreach($html->find('span[class=gs_ctc]') as $link){
    $link = $link->next_sibling();
    echo $link->plaintext;
    echo $link->href;
}

2 番目のリンクについては、次の子です<div>

foreach($html->find('div[class=gs_ggs gs_fl]') as $docLink){
    $link = $docLink->first_child();
    echo $link->href;
}

編集:2番目のリンクは最初のリンクとグループ化されているため、これを試すことができます:

foreach($html->find('span[class=gs_ctc]') as $link){
    foreach($link->parent()->parent()->find('div[class=gs_ggs gs_fl]') as $docLink){
        $link1 = $link->next_sibling();
        $link2 = $docLink->first_child();
        if(preg_match('/\.pdf$/i', $link2->href) === 1){
            echo $link1->plaintext;
            echo $link1->href;
            echo $link2->href;
        }
    }
}
于 2012-07-18T03:25:49.147 に答える