easily image crawling with python and save in local drive

2020. 3. 6. 17:51Computer Science/Machine Learning💻

반응형

In this posting, we will learn how Python can easily crawl and save images to files.

First, search the Google Search window for words to search for images.

(In my case, I searched the 'plant' because I needed plants.)

 

After navigating to the Image column, enter ctrl+shift+j to see the console window.

You can enter the following code there.

urls=Array.from(document.querySelectorAll('.rg_i')).map(el=> el.hasAttribute('data-src')?el.getAttribute('data-src'):el.getAttribute('data-iurl'));
window.open('data:text/csv;charset=utf-8,' + escape(urls.join('\n')));

Once you have entered the code and run it, the file will be downloaded. You can download it in the form of csv.

 

If you check the csv, you can see that the image URL is attached to each cell.
I'll try to save it to my local drive via Python code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import csv
import urllib.request
import os
 
num=450
#make num for saving image
 
sun=open('sunburn_real.csv','r',encoding='utf-8')
#open the csv/rename csv file what you make
 
sunurl = csv.reader(sun)
#read csv var
 
for line in sunurl:
    try:
        outpath='C:/Users/lur06/Desktop/peoplespace/'
        #write your own local location
        out="sunburn"
        file=".jpg"
        outfile=out+str(num)+file
        #set your img name
        urllib.request.urlretrieve(line[0],outpath+outfile)
        #save your img in local destination using urlib
        num=num+1
    except:
    #out the code incase of error
        print("error!")
        sun.close()
        break
        
    
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

In the next posting, we will learn about machine learning models that use colab to determine plant disease.

반응형