Lesson 2: Empty file when downloading from Google Images

I used exactly the same code mentioned in Lesson 2 to download images from Google images but it downloads an empty file. I can’t understand the javascript code and not sure why it isn’t working. Did anyone else have the same issue? If yes, how did you solve?

PS: The following code was ran in the java script console:
urls = Array.from(document.querySelectorAll(’.rg_di .rg_meta’)).map(el=>JSON.parse(el.textContent).ou);
window.open(‘data:text/csv;charset=utf-8,’ + escape(urls.join(’\n’)));
image

1 Like

Try running it in an anonymous tab

2 Likes

Cool. That worked. Why does it work only on Incognito?

Probably some extension you’re using. Ad block?

I used the “Google image” to search for the teddy bear, but the download file still empty,using the script:

urls = Array.from(document.querySelectorAll(’.rg_di .rg_meta’)).map(el=>JSON.parse(el.textContent).ou);
window.open(‘data:text/csv;charset=utf-8,’ + escape(urls.join(’\n’)));

If I searched with “cn.bing.com” and then executed the script :
elements = document.querySelectorAll(’.mimg’)
var urls = [];
for (var i = 0; i < elements.length; i++) {
**var url = elements[i].getAttribute(‘src’) **
if (url&&url.includes(‘https’)) {
urls.push(URL);
}
}
window.open(‘data:text/csv;charset=utf-8,’ + escape(urls.join(’\n’)));

the download file was many lines of the same string:

function URL() { [native code] }
function URL() { [native code] }
function URL() { [native code] }

I too have that problem and I have tried going Incognito , reinstalling Chrome , nothing seems to work , the file that gets downloaded is still 0 KB . Kindly help , Thank you !!
Below is the code i used

Capture|668x126

you can use (cn.bing.com) to download images by this code.

elements = document.querySelectorAll(’.mimg’)
var urls = [];
for (var i = 0; i < elements.length; i++) {
var url = elements[i].getAttribute(‘src’)
if (url&&url.includes(‘https’)) {
urls.push(url);
}
}
window.open(‘data:text/csv;charset=utf-8,’ + escape(urls.join(’\n’)));

I am sorry but that doesn’t work for me. I used Chrome anonymous but it turned out to be the same 0 kb empt file…

Did U solve the problem eventually?

Same, the files that I downloaded are 0 bytes as well

It seems that Google has modified/optimized their image search results so it’s harder to extract image urls from the DOM attributes. I instead used DuckDuckGo image search, and modified the first line of the Javascript code to be
urls = Array.from(document.querySelectorAll('.tile--img__img')).map(el=> el.hasAttribute('data-src')? "https:"+el.getAttribute('data-src'):"https:"+el.getAttribute('src'));

Hope this works for you!

2 Likes

I found this useful and I downloaded successfully. Try my code if neccessary.
var urls=Array.from(document.querySelectorAll('.rg_i')).map(el=> el.hasAttribute('data-src')?el.getAttribute('data-src'):el.getAttribute('data-iurl')); var hiddenElement = document.createElement('a'); hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(urls.join('\n')); hiddenElement.target = '_blank'; hiddenElement.download = 'myFile.csv'; hiddenElement.click();

2 Likes

I used this, but it seems to just list the urls instead of downloading them directly to a folder. Has anyone found a way to modify the code so it directly downloads?

You just need to do that using duckduckgo au lieu de google image :

urls = Array.from(document.querySelectorAll(‘.tile–img__img’)).map(el=> el.hasAttribute(‘data-src’)? “https:”+el.getAttribute(‘data-src’):“https:”+el.getAttribute(‘src’));
window.open(‘data:text/csv;charset=utf-8,’ + escape(urls.join(‘\n’)));

Or Also

// Google Images
var urls=Array.from(document.querySelectorAll(“.rg_i”)).map(el=>el.hasAttribute(“data-src”)?el.getAttribute(“data-src”):el.getAttribute(“data-iurl”)).filter(l=>l!=null).join(“\n”);
var a = document.createElement(“a”);a.download = “filename.txt”;a.href = “data:text/csv;charset=utf-8,”+urls;a.click();

// DuckDuckGo Images (uses Bing)
var urls=Array.from(document.querySelectorAll(“.tile–img__img”)).map(el=>el.hasAttribute(“data-src”)?el.getAttribute(“src”):el.getAttribute(“data-src”)).filter(l=>l!=null).map(l=>“https:”+l).join(“\n”);
var a = document.createElement(“a”);a.download = “filename.txt”;a.href = “data:text/csv;charset=utf-8,”+urls;a.click();

// Bing Images
var urls=Array.from(document.querySelectorAll(“.mimg”)).map(el=>el.hasAttribute(“src”)?el.getAttribute(“src”):null).filter(l=>l!=null&&l.startsWith(“http”)).join(“\n”);
var a = document.createElement(“a”);a.download = “filename.txt”;a.href = “data:text/csv;charset=utf-8,”+urls;a.click();