Path: blob/master/node_modules/@bochilteam/scraper/lib/esm/images/google-image.js
1126 views
import cheerio from 'cheerio';1import got from 'got';2export default async function googleImage(query) {3const data = await got(`https://www.google.com/search?q=${query}&tbm=isch`, {4headers: {5accept: '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',6'accept-encoding': 'gzip, deflate, br',7'accept-language': 'en-US,en;q=0.9,id;q=0.8',8'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'9}10}).text();11const $ = cheerio.load(data);12const pattern = /\[1,\[0,"(?<id>[\d\w\-_]+)",\["https?:\/\/(?:[^"]+)",\d+,\d+\]\s?,\["(?<url>https?:\/\/(?:[^"]+))",\d+,\d+\]/gm;13const matches = $.html().matchAll(pattern);14const decodeUrl = (url) => decodeURIComponent(JSON.parse(`"${url}"`));15return [...matches]16.map(({ groups }) => decodeUrl(groups === null || groups === void 0 ? void 0 : groups.url))17.filter((v) => /.*\.jpe?g|png$/gi.test(v));18}19//# sourceMappingURL=google-image.js.map2021