How to save Google search result to txt file

by srizer
2 replies
Hi

Can any one help me in how to save google search results to txt file
For e.g I will search in google seo backlink it will show result page I wish to save the url which are shown on google result page


Thanks
#file #google #result #save #search #txt
  • Profile picture of the author lee schmidt
    <?PHP

    $query = 'dogs';

    $data = getPage('http://www.google.com/search?q='.urlencode($query));

    preg_match_all("/\<li class\=g\>\<h3 class\=\"r\"\>\<a href\=\"([^\<\>]*)\" class\=l\>/", $data, $matches);

    file_put_contents('urls.txt', implode("\n", $matches[1]));

    function getPage($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);

    $data = curl_exec($ch);

    curl_close($ch);

    return $data;
    }

    ?>
    {{ DiscussionBoard.errors[2405239].message }}
  • Profile picture of the author max2010
    coooool... thanks!
    I made a little modify for saving more results
    (here is just a print out of the results, of course they can be saved in a file or in a db)
    I'm wondering if google can ban the ip for such a search way, do you think is better to add some delay?

    $query = 'dogs';
    $searches=100; // number of results

    $start=0;
    $pos=1;

    while ($start<$searches) {

    $data = getPage('http://www.google.com/search?start='.$start.'&q='.urlencode($query));

    preg_match_all("/\<li class\=g\>\<h3 class\=\"r\"\>\<a href\=\"([^\<\>]*)\" class\=l\>/", $data, $matches);

    for ($x=0; $x<count($matches[1]); $x++) {

    echo '<p>'.$pos.' '.($matches[1][$x]).'</p>';

    $pos++;

    }

    $start+=10;

    }
    {{ DiscussionBoard.errors[2719669].message }}

Trending Topics