2016-03-05 4 views
6

में Google छवि खोज परिणामों को डाउनलोड करने के लिए कैसे करें इस सवाल को कई बार पहले पूछा गया है, लेकिन सभी उत्तर कम से कम दो वर्ष पुराने हैं और वर्तमान में AJAX.googleapis.com API पर आधारित हैं, जो अब समर्थित नहीं है।पाइथन

क्या किसी को किसी अन्य तरीके से पता है? मैं एक सौ या तो खोज परिणाम डाउनलोड करने की कोशिश कर रहा हूं, और पायथन एपीआई के अतिरिक्त मैंने यह करने के लिए कई डेस्कटॉप, ब्राउज़र-आधारित, या ब्राउज़र-एडन प्रोग्रामों की कोशिश की है जो सभी विफल हो गए हैं।

धन्यवाद!

+1

आप सेलेनियम की कोशिश की है अनुकूलित करने के लिए? –

+0

'Google छवि खोज परिणामों' से आपका क्या मतलब है? – wong2

+0

सेलेनियम इसे हल किया! मैंने स्क्रॉलिंग कोड में थोड़ा बदलाव के साथ कोड https://simplypython.wordpress.com/2015/05/18/saving-images-from-google-search-using-selenium-and-python/ कोड का उपयोग किया। (पृष्ठ के निचले हिस्से में सीधे कूदना * नहीं * आवश्यक रूप से आलसी लोड किए गए पृष्ठ को सभी छवियों को लोड करने का कारण बनता है, इसलिए मैंने इसे धीरे-धीरे स्क्रॉल किया।) – xanderflood

उत्तर

4

आप जो हासिल करना चाहते हैं उसके लिए Google Custom Search का उपयोग करें। @ i08in के "Python - Download Images from google Image search?" का उत्तर देखें, इसमें महान वर्णन, स्क्रिप्ट नमूने और पुस्तकालय संदर्भ हैं।

शुभकामनाएं!

+0

मैं इसे स्वीकार कर रहा हूं क्योंकि यह निश्चित रूप से प्रश्न का उत्तर देता है! मैं भी यह इंगित करना चाहते हैं कि Google के एपीआई के पास उन उदाहरणों का उपयोग करने के लिए डिज़ाइन किए गए प्रतिबंध हैं जिन्हें उदाहरण के लिए खोज परिणामों के संग्रह को स्वचालित करने के लिए किया गया है, इसलिए यह दृष्टिकोण अनुमति समस्याओं में चलाया जा सकता है। सेलेनियम का उपयोग करने के लिए मॉर्गन जी के सुझाव ने मेरे लिए बहुत अच्छा काम किया! – xanderflood

0

आपको कस्टम खोज API का उपयोग करने की आवश्यकता है। यहां एक आसान explorer है। मैं urllib2 का उपयोग करें। आपको डेवलपर कंसोल से अपने एप्लिकेशन के लिए एपीआई कुंजी भी बनाने की आवश्यकता है।

2

मैं गूगल खोज के चित्रों डाउनलोड करने के लिए इस स्क्रिप्ट का उपयोग किया गया है और मैं उन्हें अपने मेरी classifiers trainig के लिए उपयोग किया गया है नीचे कोड क्वेरी से संबंधित 100 छवियों डाउनलोड कर सकते हैं

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 
2

किसी भी डाउनलोड करने के लिए सेलेनियम का उपयोग करके Google चित्र खोज के चित्रों की संख्या:

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
import os 
import json 
import urllib2 
import sys 
import time 

# adding path to geckodriver to the OS environment variable 
# assuming that it is stored at the same path as this script 
os.environ["PATH"] += os.pathsep + os.getcwd() 
download_path = "dataset/" 

def main(): 
    searchtext = sys.argv[1] # the search query 
    num_requested = int(sys.argv[2]) # number of images to download 
    number_of_scrolls = num_requested/400 + 1 
    # number_of_scrolls * 400 images will be opened in the browser 

    if not os.path.exists(download_path + searchtext.replace(" ", "_")): 
     os.makedirs(download_path + searchtext.replace(" ", "_")) 

    url = "https://www.google.co.in/search?q="+searchtext+"&source=lnms&tbm=isch" 
    driver = webdriver.Firefox() 
    driver.get(url) 

    headers = {} 
    headers['User-Agent'] = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36" 
    extensions = {"jpg", "jpeg", "png", "gif"} 
    img_count = 0 
    downloaded_img_count = 0 

    for _ in xrange(number_of_scrolls): 
     for __ in xrange(10): 
      # multiple scrolls needed to show all 400 images 
      driver.execute_script("window.scrollBy(0, 1000000)") 
      time.sleep(0.2) 
     # to load next 400 images 
     time.sleep(0.5) 
     try: 
      driver.find_element_by_xpath("//input[@value='Show more results']").click() 
     except Exception as e: 
      print "Less images found:", e 
      break 

    # imges = driver.find_elements_by_xpath('//div[@class="rg_meta"]') # not working anymore 
    imges = driver.find_elements_by_xpath('//div[contains(@class,"rg_meta")]') 
    print "Total images:", len(imges), "\n" 
    for img in imges: 
     img_count += 1 
     img_url = json.loads(img.get_attribute('innerHTML'))["ou"] 
     img_type = json.loads(img.get_attribute('innerHTML'))["ity"] 
     print "Downloading image", img_count, ": ", img_url 
     try: 
      if img_type not in extensions: 
       img_type = "jpg" 
      req = urllib2.Request(img_url, headers=headers) 
      raw_img = urllib2.urlopen(req).read() 
      f = open(download_path+searchtext.replace(" ", "_")+"/"+str(downloaded_img_count)+"."+img_type, "wb") 
      f.write(raw_img) 
      f.close 
      downloaded_img_count += 1 
     except Exception as e: 
      print "Download failed:", e 
     finally: 
      print 
     if downloaded_img_count >= num_requested: 
      break 

    print "Total downloaded: ", downloaded_img_count, "/", img_count 
    driver.quit() 

if __name__ == "__main__": 
    main() 

पूर्ण कोड here है।

+0

काम नहीं कर रहा है, क्या आप संशोधित कर सकते हैं? –

+0

क्या आप कृपया जो त्रुटि प्राप्त कर रहे हैं उसे बता सकते हैं? – atif93

+1

मैंने कोड बदल दिया है, इसे अभी काम करना चाहिए। – atif93

1

इस बारे में कैसे?

https://github.com/hardikvasa/google-images-download

यह आप छवियों के सैकड़ों डाउनलोड करने के लिए अनुमति देता है और से चुनने के लिए फिल्टर के एक टन है अपनी खोज को