无法刮取搜索结果,谷歌不断改变html结构

My goal is to scrape search results with PHP Simple HTML DOM Parser which is working fine for me. But after every one or two days, Google changes their HTML structure and my code stop working.

Here's my code that was working before:

include("simple_html_dom.php");
$data = file_get_contents('https://www.google.com/search?q=stackoverflow');
$html = str_get_html($data);
$i=0;
$linkObjs = $html->find('h3[class=r] a');
foreach ($linkObjs as $linkObj) {
    $i++;
    $url = trim($linkObj->href);
    $trim = substr($url, 0, 7);
    if ($trim=="/url?q=") {
        $url = substr($url, 7);
    }
    $trim_2 = stripos($url, '&sa=U');
    if ($trim_2 != false) {
        $url = substr($url, 0, $trim_2);
    }
    echo "$i:".$url.'<br>';
}

They usually change class names and tag name along with HTML links structure

I had the same problem. Try

$linkObjs = $html->find('div[class=jfp3ef] a');

and it will work again.