I wish to scrape all the images shown in the following URL: happiness
I tried many ways but I am able to fetch only 20 images. Below is the code in Python for the same:
query = input("happiness")# you can change the query for the image here
image_type="ActiOn"
query= query.split()
query='+'.join(query)
url="https://www.google.co.in/search?q="+query+"&source=lnms&tbm=isch"
print(url)
#add the directory for your image here
DIR="Pictures"
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)
if not os.path.exists(DIR):
os.mkdir(DIR)
DIR = os.path.join(DIR, query.split()[0])
if not os.path.exists(DIR):
os.mkdir(DIR)
images = [a['src'] for a in soup.find_all("img", {"src":
re.compile("gstatic.com")})]
print(images)
print("there are total" , len(images),"images")
image_type = "Action"
#print images
for img in images:
raw_img = urlopen(img).read()
#add the directory for your image here
DIR="C:\\Users\\dhvani\\Pictures\\"+query+"\\"
cntr = len([i for i in os.listdir(DIR) if image_type in i]) + 1
print(cntr)
f = open(DIR + image_type + "_"+ str(cntr)+".jpg", 'wb')
f.write(raw_img)
f.close()
Can anybody help me to extract all the images?