2016-06-13 36 views
0

我試圖通過Siterequest獲得在谷歌索引的網頁:簡單的HTML DOM - 獲取谷歌索引

因此我有一個功能叫做get_serps:

public function get_serps($pages, $start, $query) 
    { 
    //Added temporaly for Debug 
    $query = 'site:test.de'; 
    //Get Simple Html Dom 
    $parser = $this->container->get('simple_html_dom'); 

    $googleurl = 'http://www.google.de/search?num=100&start='.$start.'&hl=de&safe=off&q='.$query; 
    echo "<pre>" . $googleurl . "</pre>"; 
    $html = $parser->file_get_html($googleurl); 

    foreach ($html->find('#ires g r a') as $link) { 
     echo '</br> 2 </br>'; 
     $linkurl = $link->href; 
     echo $linkurl.'</br>'; 
     preg_match_all('#http(s)?://\b[^&]*(.*?)#', $linkurl, $target); 
     ++$count; 
    } 

    $next = $parser->modified_find('#nav tbody tr', 0); 
    $next = is_object($next) ? $next->last_child() : ''; 
    echo $next; 
    if (!empty($next) && $next->find('a')) { 
     $parser->clear(); 
     unset($parser); 
     $this->get_serps($pages, $start + 100, $query); 
    } else { 
     echo 'Count: '. $count; 
     return $count; 
    } 
} 

的問題查找(」 #ires gr a')沒有得到任何結果。

只是一個空數組...

查找功能是從Simple Html Dom Package

這是我收到的錯誤:

調用一個成員函數modified_find()上的空

原因是在find函數中返回了一個空數組。 但我不知道爲什麼該功能找不到任何東西。

function find($selector, $idx=null, $lowercase=false) 
{ 
    echo 'Selector: ' . $selector . '</br>'; 
    $selectors = $this->parse_selector($selector); 

    if (($count=count($selectors))===0) return array(); 
    $found_keys = array(); 

    // find each selector 
    for ($c=0; $c<$count; ++$c) 
    { 
     // The change on the below line was documented on the sourceforge code tracker id 2788009 
     // used to be: if (($levle=count($selectors[0]))===0) return array(); 
     if (($levle=count($selectors[$c]))===0) return array(); 
     if (!isset($this->_[HDOM_INFO_BEGIN])) return array(); 

     $head = array($this->_[HDOM_INFO_BEGIN]=>1); 

     // handle descendant selectors, no recursive! 
     for ($l=0; $l<$levle; ++$l) 
     { 
      $ret = array(); 
      foreach ($head as $k=>$v) 
      { 
       $n = ($k===-1) ? $this->dom->root : $this->dom->nodes[$k]; 
       //PaperG - Pass this optional parameter on to the seek function. 
       $n->seek($selectors[$c][$l], $ret, $lowercase); 
      } 
      $head = $ret; 
     } 

     foreach ($head as $k=>$v) 
     { 
      if (!isset($found_keys[$k])) 
      { 
       $found_keys[$k] = 1; 
      } 
     } 
    } 

    // sort keys 
    ksort($found_keys); 

    $found = array(); 
    foreach ($found_keys as $k=>$v) 
     $found[] = $this->dom->nodes[$k]; 
     var_dump($found); 

    // return nth-element or array 
    if (is_null($idx)) return $found; 
    else if ($idx<0) $idx = count($found) + $idx; 

    return (isset($found[$idx])) ? $found[$idx] : null; 

} 

整個想法是建立在Symfony框架!

回答

0
Call to a member function modified_find() on null 

錯誤中明確規定,即find()是沒有問題的,但沒有被定義的事實,您在石灰$parser稱之爲一個null-object

$next = $parser->modified_find('#nav tbody tr', 0); 

。關鍵是這樣的:

$html = $parser->file_get_html($googleurl); 

你得到你的結果$html代替$parser併爲此你需要使用發現上:

$next = $html->modified_find('#nav tbody tr', 0); 
+0

是的,我修改了代碼,上傳這樣的功能,我是調用是modified_find(),對此抱歉。 我修復了即時通訊中的錯誤,但遇到了一個新問題......我沒有得到我需要的搜索結果,我也不知道爲什麼...查找功能什麼也沒找到...... – Traxstar

+0

您是否檢查過你想要tu使用的html語法實際上有效嗎?我可以在Google搜索中找到id爲'ires'的div,而不是Elemtns'g'或'r'。我甚至不知道HTML中的元素是什麼。 – DocRattie

+0

是的。克是一個類,r是一個錯誤,而不是我用h3標籤,然後錨內,但我找不到結果 – Traxstar