select by class and id in simple html dom not work in google search

Viewed 155

The following code where I try to find divs by class is not working for google search results, I have also tried for id.

include('simple_html_dom.php');

$dom = file_get_html("https://www.google.com/search?q=best+mug");

$all_divs = $dom->find("div[class='g']");

foreach ($all_divs as $div) {
    echo $div->plaintext;
}
1 Answers

I think it's better to use XPath to do that, here is a sample of what your code could look like with XPath:

$dom = file_get_contents("https://www.google.com/search?q=best+mug");
@$doc = new DOMDocument();
@$doc->loadHTML($dom);   

$xpath = new DomXPath($doc);

$all_divs = $xpath->query("//div[@class='g']");

foreach ($all_divs as $div) {
    echo $div->plaintext;
}

Try it out and let me know if it works.

Related