const puppeteer = require('puppeteer'); const fs = require('fs'); const path = require('path'); const https = require('https'); const searchQuery = "cute cartoon character line art"; const downloadFolder = "./downloaded_images"; if (!fs.existsSync(downloadFolder)){ fs.mkdirSync(downloadFolder); } // Function to download image async function downloadImage(url, filepath){ return new Promise((resolve, reject) => { const file = fs.createWriteStream(filepath); https.get(url, (response) => { response.pipe(file); file.on('finish', () => { file.close(resolve); }); }).on('error', (err) => { fs.unlink(filepath, ()=>{}); reject(err.message); }); }); } (async () => { const browser = await puppeteer.launch({headless: false}); const page = await browser.newPage(); await page.goto('https://www.google.com'); // Accept Google cookies if needed try{ const agreeBtn = await page.$('button:contains("I agree")'); if(agreeBtn) await agreeBtn.click(); }catch(e){} // Search query await page.type('input[name=q]', searchQuery); await page.keyboard.press('Enter'); await page.waitForNavigation(); // Click Images tab const imagesTab = await page.$('a[href*="tbm=isch"]'); if(imagesTab) await imagesTab.click(); await page.waitForTimeout(3000); let achha_result_mila = false; let scrollCount = 0; while(!achha_result_mila && scrollCount < 20){ const imgUrls = await page.$$eval('img', imgs => imgs.map(i => i.src).filter(src => src && src.startsWith('http'))); for(let i=0; i

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home