Fetch Google search results for keywords

A simple script to fetch search results from Google based on keywords in your base:

let table = base.getTable('Searches');
let resultsField = table.getField('Results');
let record = await input.recordAsync('Record', table);
let searchQuery = await input.textAsync('Search query');
if (searchQuery) {
    table.updateRecordAsync(record, {[resultsField.id]: 'Updating...'});
    output.markdown(`Searching for ${searchQuery}...`);
    let results = await searchAsync(searchQuery.split(',').map(term => term.trim()).join('\n'));
    await table.updateRecordAsync(record, {[resultsField.id]: results});
    output.markdown(results);
}
async function searchAsync(searchTerm) {
    let response = await fetch(
        `https://api.apify.com/v2/actor-tasks/<TASK ID>/run-sync-get-dataset-items?token=<TOKEN>`,
        {
            method: "POST",
            headers: {"content-type": "application/json"},
            body: JSON.stringify({queries: searchTerm}),
        },
    );
    let result = await response.json();
    let resultLines = [];
    for (let {searchQuery, organicResults} of result) {
        resultLines.push(`## ${searchQuery.term}`);
        for (let {title, url, description} of organicResults) {
            resultLines.push(`- [**${title}**:](${url}) ${description}`);
        }
    }
    return resultLines.join('\n');
}
6 Likes

Very cool. Have you thought of a way to cleanly output results into another table or individual records?

Seems like this could be used to check a website’s url against these results, and then tell where your website is ranked among google search results. Could be turned into a customized SERP tracker.

1 Like

This script uses Apify’s Google Search Results Scraper—they have a lot of useful building blocks for scripts on their site!

Hey @Halley_Johnson this is great! (I think). How might I apply this if I wanted to search for a list of fellow artists’ websites if I have name and I want to search for {FullName} + “Artist” or “Art”?

Could this be used to fetch Google Image search results? We’re wanting to pull company logos.

@Brian_Swichkow - for company logos, check out clearbits free logo api: https://clearbit.com/logo

Thank you @openside!

(1) https://clearbit.com/logo as a formula field with (2) the MIniExtensions bit Convert URLs to Attachments in Bulk on Airtable, and (3) their Cloudinary bit was EXACTLY the recipe I was looking for.