Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2.2.0 / 2017-07-01
------------------
### Added
- `resultCount` field in search result

2.1.0 / 2016-08-11
------------------
### Added
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
14 changes: 14 additions & 0 deletions lib/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down