我如何只刮取Google搜索结果的第二页?

时间:2020-01-15 08:32:32

标签: php web-scraping scrape

我在抓取Google搜索结果的第二页时遇到麻烦,在抓取结果后,它将页面保存到文件中。它仅抓取第一页。 这是我的代码:

$url = 'http://www.google.com/search?q='.$in;
$datenbank = "proxy_work.php"; 
$datei = fopen($datenbank,"w+");
$datenbank = "proxy_work.php";
fwrite($datei, $url);  
fwrite ($datei,"\r\n");
fclose($datei);
// echo file_get_contents("proxy_work.php");
$html = file_get_html("proxy_work.php");
foreach($html->find('a') as $e){
    //  $title = $h3->innertext;
    $link  = $e->href;
    //  if(in_array($endomain, $approveurl)){
            // 
        }
    // if it is not a direct link but url reference found inside it, then extract
    if (!preg_match('/^https?/', $link) && preg_match('/q=(.+)&sa=/U', $link, $matches) && preg_match('/^https?/', $matches[1])) {
        $link = $matches[1];
    } else if (!preg_match('/^https?/', $link)) { // skip if it is not a valid link
        continue;
    } 
}

0 个答案:

没有答案