diff --git a/CHANGELOG.md b/CHANGELOG.md index fa346f4..c4662cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +2.2.0 / 2017-07-01 +------------------ +### Added +- `resultCount` field in search result + 2.1.0 / 2016-08-11 ------------------ ### Added diff --git a/README.md b/README.md index 92b0fd7..4075d3b 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,7 @@ The provided callback will receive a response object as second argument, it has - `body`: The HTML of the loaded page - `next`: A method that invokes the originally specified callback with next page results - `$`: A cheerio instance of the loaded page +- `resultCount`: Googles estimated result count Updating from 1.x ------- diff --git a/lib/google.js b/lib/google.js index bf5d0f0..d399a89 100644 --- a/lib/google.js +++ b/lib/google.js @@ -7,6 +7,7 @@ var linkSel = 'h3.r a' var descSel = 'div.s' var itemSel = 'div.g' var nextSel = 'td.b a span' +var resultCountSel = '#resultStats' var URL = '%s://www.google.%s/search?hl=%s&q=%s&start=%s&sa=N&num=%s&ie=UTF-8&oe=UTF-8&gws_rd=ssl' @@ -30,6 +31,8 @@ google.lang = 'en' google.requestOptions = {} google.nextText = 'Next' google.protocol = 'https' +google.resultCountRegExp = /^About ([0-9,.]+) results$/ +google.resultCountReplace = /[,.]/g var igoogle = function (query, start, callback) { if (google.resultsPerPage > 100) google.resultsPerPage = 100 // Google won't allow greater than 100 anyway @@ -93,6 +96,17 @@ var igoogle = function (query, start, callback) { } } + const match = $(resultCountSel).text().match(google.resultCountRegExp) + if (match === null) { + res.resultCount = 0 + } else { + res.resultCount = parseInt( + match[1] + .replace(google.resultCountReplace,"") + ) + } + + callback(null, res) } else { callback(new Error('Error on response' + (resp ? ' (' + resp.statusCode + ')' : '') + ':' + err + ' : ' + body), null, null) diff --git a/package.json b/package.json index c90e3c9..ba85c28 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "google", - "version": "2.1.0", + "version": "2.2.0", "description": "A module to search and scrape google. This is not sponsored, supported, or affiliated with Google Inc.", "homepage": "https://github.com/jprichardson/node-google", "repository": {