Skip to content

Instantly share code, notes, and snippets.

@FarisHijazi
Created September 8, 2021 12:14
Show Gist options
  • Save FarisHijazi/4831fe511a46579eb168a4901e20164c to your computer and use it in GitHub Desktop.
Save FarisHijazi/4831fe511a46579eb168a4901e20164c to your computer and use it in GitHub Desktop.
Google images thumbnail fetcher for google sheets script
/*
* @author https://github.com/FarisHijazi
* @param query (string): search query to send to google
*
* this script fetches a google images page and uses regex to extract thumbnail URLs
* more work can be done like extracting names and descriptions too
*/
function getGoogleImages(query) {
if (!query) return [];
var html = UrlFetchApp.fetch("https://www.google.com/search?hl=en&tbm=isch&q=" + encodeURIComponent(query.trim()), {
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-language": "en-US,en;q=0.9,ar;q=0.8",
"cache-control": "max-age=0",
"sec-ch-ua": "\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\"",
"sec-ch-ua-mobile": "?0",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
},
"referrerPolicy": "strict-origin-when-cross-origin",
"body": null,
"method": "GET",
"mode": "cors",
"credentials": "include"
}).getContentText();
return html.match(/src=\"(.*?)\"/gim).map(s => s.slice(5, -1)).slice(1);
}
@FarisHijazi
Copy link
Author

you can copy this and added to your google sheets

click Tools > Script editor and paste it

see more details here

@Masum-MSNR
Copy link

Hello, Sir. How can I increase image resolution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment