PHP 批量获取 百度搜索结果 网址列表

<?php

set_time_limit(0);



function curl($url){

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_TIMEOUT, 5);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_HEADER, 0);

    $data = curl_exec($ch);

    curl_close($ch);

    return $data;

}



function baidu_site_url($wd, $pn){

    

    $html = curl('http://www.baidu.com/s?wd='.urlencode($wd).'&pn='.$pn);

    preg_match_all('/<span class="g">([^\/]*)\//', $html, $matches);

    return $matches[1];

}



if (!empty($_GET['page']) && !empty($_GET['wd'])){//获取到第几页, 关键词

    

    $page = $_GET['page'];

    $wd = $_GET['wd'];

    $handle = fopen('url.txt', 'a');

    

    for ( $i = 0 ; $i < $page ; $i++ ) {

        

        $tmp = baidu_site_url($wd, $i*10);

        foreach ($tmp as $v){

            fwrite($handle, $v."\r");

        }

    }

    

    fclose($handle);

    

}else{

    

    exit;

}



?>

 

 

你可能感兴趣的:(PHP)