简单的html dom获取链接

时间:2016-09-23 06:52:13

标签: php simple-html-dom

如何通过简单的html dom php获取第2行的第一个链接?

    <div class"content">
//row 1
 <div class="row">
       <h2 class="headline">Movies</h2>
          <a href="http://test.com/everest-2015">
          <a href="http://test.com/everest-fly-2015">
    </div>

//row 2
   <div class="row">
       <h2 class="headline">TvShows</h2>
          <a href="http://test.com/everest-2015">
          <a href="http://test.com/everest-fly-2015">
    </div>

 </div>

有时我有一排我有两排,所以我想要一种方法来<h2> innertext TvShows

$title_show = "the flash";
     $html = file_get_html('https://tainies.online/search?q='.$title_show.'');

    $elements = $html->find("figcaption .$title_show. a");

            if(!is_null($elements)){


                foreach($elements as $element){
                //first tvshow search result
                     if ($tmp++ < 1) {
                    $query = parse_url($element, PHP_URL_QUERY);
                    parse_str($query,$op);
                    $link = base64_decode($op["url"]);  
                     $url22 =  $element->href;
        if(strpos($element->href, $title_show) !== false) {
                    $url22_season = $url22;
                        }
                     }
                    }
                }
            echo $url22."</br>";

这是页面https://tainies.online/search?q=the+flash 我试图获得 flash 2014 网址,因为你可以看到结果搜索给电影(ΤΑΙΝΙΕΣ)这个名字和电视节目(ΣΕΙΡΕΣ)

我的代码给了我电影的网址(ΤΑΙΝΙΕΣ)-justice-league- the-flash point-paradox,我需要代码从电视节目中删除第一个电视节目的网址(ΣΕΙΡΕΣ )

2 个答案:

答案 0 :(得分:0)

使用以下内容。没有经过测试但会做到这一点。

$html->find('div.row', 1)->find('h2')->innertext;

答案 1 :(得分:0)

试试这段代码:

$('.row').eq(1).find('h2').html(); 

注意:eq(1)因为它以0开头,所以第二个div将是eq(1)