npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

google-search-results-nodejs

v2.1.0

Published

Google Search Node JS API via SerpApi.com

Downloads

8,264

Readme

Google Search Node.js

npm version test

This NodeJS module is designed to scrape and parse Google, Bing and Baidu results using SerpApi. This Ruby Gem is meant to scrape and parse Google results using SerpApi. The following services are provided:

SerpApi provides a script builder to get you started quickly.

This npm package is meant to scrape and parse Google results using SerpApi. The following services are provided:

SerpApi provides a script builder to get you started quickly.

The full documentation is available here.

Link to NodeJS Package

Requirement

  • ES6 basic understanding
  • NodeJS coding skills
  • Node 7+ and NPM installed

Installation

NPM 7+

$ npm install google-search-results-nodejs

Link to npm package

Quick start

const SerpApi = require('google-search-results-nodejs')
const search = new SerpApi.GoogleSearch("Your Private Key")
search.json({
 q: "Coffee", 
 location: "Austin, TX"
}, (result) => {
  console.log(result)
})

This example runs a search about "coffee" using your secret api key.

The SerpApi service (backend)

  • searches on Google using the search: q = "coffee"
  • parses the messy HTML responses
  • return a standardizes JSON response The class GoogleSearch
  • Format the request to SerpApi server
  • Execute GET http request
  • Parse JSON into Ruby Hash using JSON standard library provided by Ruby Et voila..

Alternatively, you can search:

  • Baidu using BaiduSearch class
  • Bing using BingSearch class
  • DuckDuckGo using DuckDuckGoSearch class
  • Yahoo using YahooSearch class
  • Ebay using EbaySearch class
  • Yandex using YandexSearch class
  • HomeDepot using HomeDepotSearch class
  • GoogleScholar using GoogleScholarSearch class
  • Youtube using YoutubeSearch class
  • Walmart using WalmartSearch
  • Apple App Store using AppleAppStoreSearch class
  • Naver using NaverSearch class

See the playground to generate your code.

Example

How to set SERP API key

The SerpApi api_key can be set globally using a singleton pattern.

const SerpApi = require('google-search-results-nodejs')
let search = new SerpApi.GoogleSearch("Your Private Key")

The SerpApi api_key can be provided for each request

const SerpApi = require('google-search-results-nodejs')
let search = new SerpApi.GoogleSearch()
let result = search.json({
 api_key: "Your private key",
 q: "Coffee",            // search query
 location: "Austin, TX", // location 
}, (data) => {
  console.log(data)
})

Search API capability

query_params = {
  q: "query",
  google_domain: "Google Domain", 
  location: "Location Requested", 
  device: device,
  hl: "Google UI Language",
  gl: "Google Country",
  safe: "Safe Search Flag",
  num: "Number of Results",
  start: "Pagination Offset",
  api_key: "Your SERP API Key",  // https://serpapi.com/dashboard
  tbm: "nws|isch|shop",
  tbs: "custom to be search criteria",
  async: true|false,   // allow async query
  output: "json|html", // output format
}

const SerpApi = require('google-search-results-nodejs')
const search = new SerpApi.GoogleSearch()

// create a callback
callback = (data) => {
 console.log(data)
}

// Show result as JSON
search.json(query_params, callback)

// Show result as HTML file
search.html(query_params, callback)

the full documentation

see below for more hands on examples.

Example by specification

We love true open source, continuous integration and Test Drive Development (TDD). We are using RSpec to test our infrastructure around the clock to achieve the best QoS (Quality Of Service).

The directory test/ includes specification/examples.

Set your api key.

export API_KEY="your secret key"

Run all tests npm test

Location API

const search = new SerpApi.GoogleSearch(api_key)
search.location("Austin", 3, (data) => {
  console.log(data)
})

it prints the first 3 location matching Austin (Texas, Texas, Rochester)

[ { id: '585069bdee19ad271e9bc072',
    google_id: 200635,
    google_parent_id: 21176,
    name: 'Austin, TX',
    canonical_name: 'Austin,TX,Texas,United States',
    country_code: 'US',
    target_type: 'DMA Region',
    reach: 5560000,
    gps: [ -97.7430608, 30.267153 ],
    keys: [ 'austin', 'tx', 'texas', 'united', 'states' ] },
  ...]

Search Archive API

The first search result returns a search_id which can be provided to get the search result from the archive.

var search = new SerpApi.GoogleSearch(api_key)
search.json({q: "Coffee", location: "Portland" }, (search_result) => {
  // search in archive for the search just returned
  search.search_archive(search_result.search_metadata.id, (archived_search) => {
    console.log(archived_search)
  })
})

it prints the search from the archive.

Account API

const search = new SerpApi.GoogleSearch(api_key)
search.account((data) => {
  console.log(data)
})

it prints your account information.

Promise and callback

This API was developped using basic callback to handle response. And exception are just throw away with interceptor.

if you want to take advantage of the promise to block the request. here is how I will do.

const util = require('util')

function getJson(parameter, resolve, reject) {  
  const search = new SerpApi.GoogleSearch(api_key)
  try {
    search.json(parameter, resolve)
  } catch (e) {
    reject(e)
  }
}

const blockFn = util.promisify(getJson)
blockFn[util.promisify.custom](parameter).then((data) => {
  expect(data.local_results[0].title.length).toBeGreaterThan(5)
  done()
}).catch((error) => {
  console.error(error)
  done()
})

Reference:

  • test: test/ExampleSpec.js
  • documentation: https://nodejs.org/docs/latest-v8.x/api/util.html#util_util_promisify_original

Coding style

This API is using callback to run in non-blocking code. Here we are trying to follow the spirit of NodeJS. For reference you can read this article:

  • https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/
  • https://nodejs.org/en/docs/guides/dont-block-the-event-loop/

For pratical example, you can see the test located under test/.

Run regression

To run the regression suite.

export API_KEY="your api key"
make test

Change log

  • 2.1
    • add support for Naver, HomeDepot, AppleStoreApp, DuckDuckGo
    • defeature location if it is not supported by the search engine
  • 2.0.1
  • fix classes loading.
  • 2.0
    • Refractor class name: SearchResult -> Search
  • 1.2
    • stable version to support all the basic search API.

Conclusion

SerpApi supports Google Images, News, Shopping and more.. To enable a type of search, the field tbm (to be matched) must be set to:

  • isch: Google Images API.
  • nws: Google News API.
  • shop: Google Shopping API.
  • any other Google service should work out of the box.
  • (no tbm parameter): regular Google search.

The field tbs allows to customize the search even more.

The full documentation is available here.