Getting Google query results into array

Discussion in 'PHP' started by Cooker, Jul 14, 2010.

  1. #1
    Cooker, Jul 14, 2010 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    parse_url($url);
     
    bartolay13, Jul 14, 2010 IP
  3. Cooker

    Cooker Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Not like that, i want to catch all search result URL's into array from specified search result page
     
    Cooker, Jul 14, 2010 IP
  4. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This is how I used to do it before Google AJAX'ified their search results pages:

    
    <?php
        $keyword = 'php';
    	$search_results = file_get_contents('http://www.google.co.uk/search?hl=en&gl=uk&q='.$keyword.'&btnG=Search&meta=');
    	
        $pattern = '#<h3 class=r><a href="(.*?)"[^>]*>(.*?)<\/a>.*?<div class="s">(.*?)<br>#';
            
         // did we get any results?
         if (preg_match_all($pattern, $search_results, $links)) {
             
             for ($i=0; $i < count($links[1]); $i++) {
                 $results[$i]['url']   = $links[1][$i];
                 $results[$i]['title'] = $links[2][$i];
                 $results[$i]['descr'] = $links[3][$i];
                 
                 echo $results[$i]['url'] . "<br />\n"   . 
                      $results[$i]['title'] . "<br />\n" . 
                      $results[$i]['descr'] . "<br /><br />\n\n";
             }  
            
         }
    ?>
    
    PHP:
    I'm afraid I haven't bothered to fix it since though - might be of use to you.
    The HTML that the Ajax loads into the DOM is almost identical to the one before, so the regex should still work when you figure out how to grab the HTML :).
     
    Deacalion, Jul 15, 2010 IP