thibauts / node-google-search-scraper

Google search scraper with captcha solving support
MIT License
89 stars 37 forks source link

How to get multiple results? #6

Open gajus opened 7 years ago

gajus commented 7 years ago

The callback is called for each response.

To answer my own question,

const searchGoogle = (query: string, limit: number = 10) => {
  return new Promise((resolve, reject) => {
    const results = [];

    GoogleSearchScraper.search({
      host: 'www.google.com',
      lang: 'en',
      limit,
      query: query + ' site:imdb.com/title/'
    }, (error, result) => {
      if (error) {
        reject(error);
      } else {
        results.push(result);

        if (limit === results.length) {
          resolve(results);
        }
      }
    });
  });
};

However, this will hang if there are no results for the query.

Am I overlooking something?

thibauts commented 7 years ago

You're right, another callback should be fired when there are no more results. I'll accept PRs fixing this as long as they are backwards-compatible.