Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
@subho57/serpapi
Advanced tools
Scrape and parse search engine results using SerpApi with Browser support
This is fork of the original serpapi/serpapi-javascript repository with support for modern browsers and Node >= 18. If using Node >= 7, please use the original repository.
Scrape and parse search engine results using SerpApi. Get search results from Google, Bing, Baidu, Yandex, Yahoo, Home Depot, eBay and more.
🪧 Coming from google-search-results-nodejs ? Check out the migration document to find out how to upgrade. |
---|
npm install @subho57/serpapi
const { getJson } = require("@subho57/serpapi");
getJson({
engine: "google",
api_key: API_KEY, // Get your API_KEY from https://serpapi.com/manage-api-key
q: "coffee",
location: "Austin, Texas",
}, (json) => {
console.log(json["organic_results"]);
});
You will need to add "type": "module"
to your package.json
:
{
"type": "module",
// rest of package.json
}
import { getJson } from "@subho57/serpapi";
const response = await getJson({
engine: "google",
api_key: API_KEY, // Get your API_KEY from https://serpapi.com/manage-api-key
q: "coffee",
location: "Austin, Texas",
});
console.log(response);
import { getJson } from "https://deno.land/x/serpapi/mod.ts";
You can declare a global api_key
and timeout
value by modifying the config
object. timeout
is defined in milliseconds and defaults to 60 seconds.
All functions, other than getLocations
, accepts an optional api_key
and
timeout
that will take precedence over the values defined in config
.
getLocations
doesn't require an API key.
import { config, getJson } from "@subho57/serpapi";
config.api_key = API_KEY;
config.timeout = 60000;
await getJson({ engine: "google", q: "coffee" }); // uses the API key defined in the config
await getJson({ engine: "google", api_key: API_KEY_2, q: "coffee" }); // API_KEY_2 will be used
Search engines handle pagination in several different ways. Some rely on an "offset" value to return results starting from a specific index, while some others rely on the typical notion of a "page". These are often combined with a "size" value to define how many results are returned in a search.
This module helps you handle pagination easily. After receiving search results
from getJson
, simply call the next()
method on the returned object to
retrieve the next page of results. If there is no next()
method, then either
pagination is not supported for the search engine or there are no more pages to
be retrieved.
const page1 = await getJson({ engine: "google", q: "coffee", start: 15 });
const page2 = await page1.next?.();
You may pass in the engine's supported pagination parameters as per normal. In the above example, the first page contains the 15th to the 24th result while the second page contains the 25th to the 34th result.
Note that if you set no_cache
to true
, all subsequent next()
calls will
not return cached results.
Refer to the getJson
definition below for more examples.
Get a JSON response based on search parameters.
.next()
method on the returned
response object.parameters
object
search query parameters for the enginecallback
fn? optional callback// single call (async/await)
const json = await getJson({ engine: "google", api_key: API_KEY, q: "coffee" });
// single call (callback)
getJson({ engine: "google", api_key: API_KEY, q: "coffee" }, console.log);
// pagination (async/await)
const page1 = await getJson({ engine: "google", q: "coffee", start: 15 });
const page2 = await page1.next?.();
// pagination (callback)
getJson({ engine: "google", q: "coffee", start: 15 }, (page1) => {
page1.next?.((page2) => {
console.log(page2);
});
});
// pagination loop (async/await)
const organicResults = [];
let page = await getJson({ engine: "google", api_key: API_KEY, q: "coffee" });
while (page) {
organicResults.push(...page.organic_results);
if (organicResults.length >= 30) break;
page = await page.next?.();
}
// pagination loop (callback)
const organicResults = [];
getJson({ engine: "google", api_key: API_KEY, q: "coffee" }, (page) => {
organicResults.push(...page.organic_results);
if (organicResults.length < 30 && page.next) {
page.next();
}
});
Get a HTML response based on search parameters.
parameters
object
search query parameters for the enginecallback
fn? optional callback// async/await
const html = await getHtml({ engine: "google", api_key: API_KEY, q: "coffee" });
// callback
getHtml({ engine: "google", api_key: API_KEY, q: "coffee" }, console.log);
Get a JSON response given a search ID.
search_metadata.id
key in the
response.async
parameter.const response = await getJson({
engine: "google",
api_key: API_KEY,
async: true,
q: "coffee",
});
const { id } = response.search_metadata;
await delay(1000); // wait for the request to be processed.
// async/await
const json = await getJsonBySearchId(id, { api_key: API_KEY });
// callback
getJsonBySearchId(id, { api_key: API_KEY }, console.log);
Get a HTML response given a search ID.
search_metadata.id
key in the
response.async
parameter.const response = await getJson({
engine: "google",
api_key: API_KEY,
async: true,
q: "coffee",
});
const { id } = response.search_metadata;
await delay(1000); // wait for the request to be processed.
// async/await
const html = await getHtmlBySearchId(id, { api_key: API_KEY });
// callback
getHtmlBySearchId(id, { api_key: API_KEY }, console.log);
Get account information of an API key. https://serpapi.com/account-api
parameters
object
(optional, default {}
)
callback
fn? optional callback
// async/await
const info = await getAccount({ api_key: API_KEY });
// callback
getAccount({ api_key: API_KEY }, console.log);
Get supported locations. Does not require an API key. https://serpapi.com/locations-api
parameters
object
(optional, default {}
)
callback
fn? optional callback
// async/await
const locations = await getLocations({ limit: 3 });
// callback
getLocations({ limit: 3 }, console.log);
FAQs
Scrape and parse search engine results using SerpApi with Browser support
We found that @subho57/serpapi demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.