To retrieve all of the Google search results for a given query string, you need to set up a do/while loop within n8n. This approach expands my workflow’s Google Search to return more than 10 results per call. ๐
Below is JSON that I run as a sub-workflow, it does six things:
Initialize with Inputcode node creates the necessary values for the workflow to run- CX string required by the Google Custom Search API.
- The
queryto run – I pass this into the sub-workflow, but I set up a default value for testing/debugging purposes. In a non-personal environment, I wouldn’t do this. ๐ - And it initializes various ‘loop variables’ (
start,allResults,hasMoreResults,totalResults,currentPage, andinputData).
Google Search Requestis anhttpRequestnode that executes the query using the following query parameters:keypasses your Google API key
ToDo – I would like to store this in the n8n credentials, but I can’t figure out how yet. ๐- The other required search parameters, including
start, which tells Google which page of results to return.
Process Resultscode node examines the output from thehttpRequestand updates all of the relevant ‘loop variables’ and concatenates the items returned fromhttpRequestto theallResultsand updates relevant variables for the next run (if one is needed).Should Continue?is anifnode that checkshasMoreResultsfrom the prior node to determine if we need to retrieve more results, or whether we are done.- If we are not done, and
Should Continue?returns true, we hit a pause node (Rate Limit Wait) to not overwhelm the API, and then we go to theGoogle Search Requestnode to request more results using the updated variables set in theProcess Resultsnode. - Once we are done,
Prepare Outputformats the JSON that we will return back to the calling workflow
In the parent workflow, this is what it looks like to call this workflow:
- Set the search parameters - This is optional; I do this as a separate step because I use the same search string for multiple site searches.
- I call the Google Search subworkflow and pass the domain name in the
siteSearchinput field. - Process the search results, extracting relevant summary values that I use later. This node is optional.
- Split out the single result set into a collection of results that I can use in merge/combine nodes later
This approach works for me because I tightly scope my search string and the domain. As you experiment with it, consider capping the total number of results or pages that you retrieve from Google.
I hope that this helps you do more with your n8n workflows. If you have questions on this approach or suggestions on how to do it better, let me know!
Side Note: This is the first post to use a WordPress plugin I built to display n8n workflow JSON code visually using the n8n-demo-component project - I hope you enjoy it!
Pingback: How to use n8n to Search Job Boards - Cliff Simpkins