-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscrape_image.py
More file actions
70 lines (63 loc) · 3.03 KB
/
scrape_image.py
File metadata and controls
70 lines (63 loc) · 3.03 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from bs4 import BeautifulSoup
import requests
import re
import os
import argparse
import sys
import json
from PIL import Image
from io import BytesIO
import urllib.request, random, imghdr, time
###
# adapted from http://stackoverflow.com/questions/20716842/python-download-images-from-google-image-search
# This script takes in queries to search on google images. It then takes the first 20 images of the query and
# attempts to download it. The queries can be inputed through argparse or by editing the file directly.
###
# Send the request to the url, using the input header
def get_soup(url,header):
return BeautifulSoup(urllib.request.urlopen(urllib.request.Request(url,headers=header)),'html.parser')
# Main function: Queries google images with each input query and downloads images
def main(args):
default_parent_folder = "scraped_images"
cwd = os.getcwd()
parent_folder = os.path.join(cwd, default_parent_folder)
if not os.path.exists(parent_folder):
os.mkdir(parent_folder)
if args.query is not None:
class_name = args.folder_name
queries = args.query
else:
class_name = "dog"
queries = ["dogs", "pug", "puppy"]
save_directory = os.path.join(parent_folder, class_name)
# for each of the input query, perform search and save the image.
for query in queries:
max_images = 100
query= query.replace(" ", "+")
url="https://www.google.co.in/search?q="+query+"&source=lnms&tbm=isch"
header={'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36"}
soup = get_soup(url,header)
ActualImages=[]# contains the link for Large original images, type of image
for a in soup.find_all("div",{"class":"rg_meta"}):
link , Type =json.loads(a.text)["ou"] ,json.loads(a.text)["ity"]
ActualImages.append((link,Type))
counter = 0
for i , (img , _) in enumerate( ActualImages):
try:
raw_img = Image.open(BytesIO(requests.get(img, timeout=30).content))
raw_img.save(os.path.join(save_directory, "{}{}.{}".format(query, counter, raw_img.format.lower())))
counter += 1
if counter >= max_images:
break
raw_img.close()
except:
pass
print("Cycle for " + query + " done")
print("All Images have been downloaded.")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--query", nargs='+', help="name of query to search for")
parser.add_argument("--folder_name", default="query", help="name of folder to store scraped images")
args = parser.parse_args()
main(args)
sys.exit()