serp-spider / search-engine-google

:spider: Google client for SERPS
https://serp-spider.github.io
Other
165 stars 61 forks source link

getNumberOfResults() returns the page number #100

Closed migliori closed 6 years ago

migliori commented 6 years ago

Hi,

Thanks for your great work.

I suppose Google has changed the way it shows the total number of results: $response = $google_client->query($google_url); $number_of_results = $response->getNumberOfResults(); Now returns the page number instead of the number of results if we're not on 1st page.

I changed getNumberOfResults() in search-engine-google/src/Page/GoogleSerp.php this way:

   // $matched = preg_match('/([0-9]+[^0-9]?)+/u', $nodeText->textContent, $countMatch);

    // if (!$matched) {
    //     return null;
    // }

    // return (int) preg_replace('/[^0-9]/', '', $countMatch[0]);

    if (!preg_match_all('/([0-9]+[^0-9]?)+/u', $nodeText->textContent, $match, PREG_PATTERN_ORDER)) {
        return null;
    }

    $resultIndex = count($match[0]) - 1;

    return (int) preg_replace('/[^0-9]/', '', $match[0][$resultIndex]);

`

This way it matches "Page x of about xxx results" as well as "about xxx results"

gsouf commented 6 years ago

Hi @migliori

Thanks for reporting and for your example. I'll get this fixed

migliori commented 6 years ago

You're welcome, and again thanks to you for your excellent work - I use it at https://www.hack-hunt.com/find-illegal-shared-links.html

gsouf commented 6 years ago

Hi @migliori that's implemented in version 0.4.1

I'm glad to know it serves you to achieve a great work!

migliori commented 6 years ago

Great, thanks!