Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to download multiple googel images using python

from bs4 import BeautifulSoup
import requests
import re
import urllib2
import os
import cookielib
import json

def get_soup(url,header):
    return BeautifulSoup(urllib2.urlopen(urllib2.Request(url,headers=header)),'html.parser')


query = raw_input("query image")# 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)


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))

print  "there are total" , len(ActualImages),"images"

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)
###print images
for i , (img , Type) in enumerate( ActualImages):
    try:
        req = urllib2.Request(img, headers={'User-Agent' : header})
        raw_img = urllib2.urlopen(req).read()

        cntr = len([i for i in os.listdir(DIR) if image_type in i]) + 1
        print cntr
        if len(Type)==0:
            f = open(os.path.join(DIR , image_type + "_"+ str(cntr)+".jpg"), 'wb')
        else :
            f = open(os.path.join(DIR , image_type + "_"+ str(cntr)+"."+Type), 'wb')


        f.write(raw_img)
        f.close()
    except Exception as e:
        print "could not load : "+img
        print e
Comment

PREVIOUS NEXT
Code Example
Python :: python-crontab sheduling at specific time 
Python :: python von konsoleeinlesen 
Python :: features and image recongnition 
Python :: colab not training always giving cuda out of memory error eventhough memory is available 
Python :: python -m pip install --upgrade pip /usr/bin/python: No module named pip 
Python :: English Dictionary labels 
Python :: drop values based on type pandas 
Python :: can we pickle pyspark dataframe using python 
Python :: norm 2 or ocklidos of matrix in python 
Python :: JEW token authentication in Django UTC 
Python :: spacy print word in vocab 
Python :: python convert a dict to list or a list to dict or a slice a dict or sort a dict by key or value without import 
Python :: what does - none do in python 
Python :: Using np.unravel_index on argmax output 
Python :: python enumerate list with comprehension 
Python :: tessa thompson 
Python :: semicircle 
Python :: list into string python 
Python :: all classification algorithim compare 
Python :: get method to create a set of counters in python 
Python :: Make exact copy of an environment 
Python :: Python - Cómo comprobar si dos cuerdas son anagramas 
Python :: extract area code from phone number python 
Python :: display all rows pandas 
Python :: odoo - add one2many field programmatically 
Python :: spark group by alias 
Python :: delete row by index pandas 
Python :: flask new response style with `make_response` 
Python :: pandas 3d tutorail pythoin 
Python :: python import file from same directory 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =