Google Image Download By Python

Dear all:

  • I wrote a simple Python Script to download the Google Images.
  • It could download all images of a specific Search Term on Google Image webpage
  • How to set the environment and how to use it is on below
  • I hope it could make some help for Sephra's project
  • As J also use Python, I think he can understand the script and set the environment

1. Python Version and Libraries

1.1 Version

  • Python 3.6 with anaconda3
  • Chrome browser

1.2 Libraries

  • selenium : Could be installed easily by pip
    • Reference : (https://github.com/SeleniumHQ/selenium)
    • Notation : As the Google Image Search APIs could not be used since 2011, many developers now using this library to develop webpage crawlers
pip install selenium

2. Full Code

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
import urllib
import sys

# search URL
# 要搜索的網(wǎng)址 
Url_Behind = "https://www.google.co.jp/search?q="
Url_SearchTerm = sys.argv[1] # 要搜索的詞條 Search Term
Url_After = "&newwindow=1&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjTqs6EwPDXAhXCa7wKHQzNAn0Q_AUICigB&biw=1920&bih=1069"

searchUrl = Url_Behind + Url_SearchTerm + Url_After
print(searchUrl)

# Chrome驅(qū)動器下載屠列,配置過程詳見下個章節(jié)
# Chrome Driver, How to configure it, please see next chapter 
path = 'D:\\DATA\\chromedriver.exe'

# start Chrome
# 啟動 Chrome
driver = webdriver.Chrome(path)

# Maximize the website window
# 網(wǎng)站窗口最大化, 因為每次爬取只能看到視窗內(nèi)的圖片
driver.maximize_window()

# start search
# 開始搜索
driver.get(searchUrl)

# scroll postion
# 模擬滾動窗口以瀏覽下載更多圖片 
pos = 0

# image id, 要下載的圖片編號
m = 0 

img_url_list = []

for i in range(20):
    pos += i*500 # scroll down
    js = "document.documentElement.scrollTop=%d" % pos
    driver.execute_script(js)
    time.sleep(1)
    # Find Image
    # 找到圖片
    ELEMENT = driver.find_elements_by_tag_name("img")

    for element in ELEMENT:
        # find image download URL
        # 找到圖片下載地址
        IMG_URL = element.get_attribute('src')
        if type(IMG_URL) == str and IMG_URL[8:17] == "encrypted":
            img_url = IMG_URL
            if img_url != None and (img_url not in img_url_list):
                img_url_list.append(img_url)
                m += 1
                # Image save Dir and filename,could set by your self
                # 圖片保存地址和圖片名,可以自己設(shè)置
                filename = "D:\\DATA\\PicCrawler\\" + Url_SearchTerm + str(m) + ".jpg"
                urllib.request.urlretrieve(img_url,filename)
                print("Save Picture %s" %filename)
    # show more images on webpage
    # 點擊網(wǎng)頁的“顯示更多圖片按鈕”讲竿,顯示更多圖片繼續(xù)下載
    click_btn = driver.find_element_by_id('smb')
    ActionChains(driver).click(click_btn).perform()

# Close webpage
# 關(guān)閉網(wǎng)頁
driver.close()

3. Usage

3.1 Download Chrome driver

path = 'D:\\DATA\\chromedriver.exe'
  • You can set the path by yourself

3.2 Install selenium library

pip install selenium

3.3 Start Download

  • You can execute the script with Term you want to search
  • For instants, if you want to download the image of Soccer you can use like this:
python filename.py soccer

4. Discussion

  • When you search a Term on Google Image, it usually only show less than 1000 images(Don't know Why!), The script only can download all the showed images, So if we can let the webpage show more images when search a Term, we can download more images.

Sorry for absent our discussion, Hope it could help.**

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末淋叶,一起剝皮案震驚了整個濱河市禀倔,隨后出現(xiàn)的幾起案子章郁,更是在濱河造成了極大的恐慌椿猎,老刑警劉巖危尿,帶你破解...
    沈念sama閱讀 216,843評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件食棕,死亡現(xiàn)場離奇詭異朗和,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)簿晓,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,538評論 3 392
  • 文/潘曉璐 我一進(jìn)店門眶拉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人憔儿,你說我怎么就攤上這事忆植。” “怎么了?”我有些...
    開封第一講書人閱讀 163,187評論 0 353
  • 文/不壞的土叔 我叫張陵朝刊,是天一觀的道長耀里。 經(jīng)常有香客問我,道長拾氓,這世上最難降的妖魔是什么冯挎? 我笑而不...
    開封第一講書人閱讀 58,264評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮咙鞍,結(jié)果婚禮上房官,老公的妹妹穿的比我還像新娘。我一直安慰自己续滋,他們只是感情好翰守,可當(dāng)我...
    茶點故事閱讀 67,289評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著疲酌,像睡著了一般蜡峰。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上朗恳,一...
    開封第一講書人閱讀 51,231評論 1 299
  • 那天事示,我揣著相機(jī)與錄音,去河邊找鬼僻肖。 笑死,一個胖子當(dāng)著我的面吹牛卢鹦,可吹牛的內(nèi)容都是我干的臀脏。 我是一名探鬼主播,決...
    沈念sama閱讀 40,116評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼冀自,長吁一口氣:“原來是場噩夢啊……” “哼揉稚!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起熬粗,我...
    開封第一講書人閱讀 38,945評論 0 275
  • 序言:老撾萬榮一對情侶失蹤搀玖,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后驻呐,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體灌诅,經(jīng)...
    沈念sama閱讀 45,367評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,581評論 2 333
  • 正文 我和宋清朗相戀三年含末,在試婚紗的時候發(fā)現(xiàn)自己被綠了猜拾。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,754評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡佣盒,死狀恐怖挎袜,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤盯仪,帶...
    沈念sama閱讀 35,458評論 5 344
  • 正文 年R本政府宣布紊搪,位于F島的核電站,受9級特大地震影響全景,放射性物質(zhì)發(fā)生泄漏耀石。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,068評論 3 327
  • 文/蒙蒙 一蚪燕、第九天 我趴在偏房一處隱蔽的房頂上張望娶牌。 院中可真熱鬧,春花似錦馆纳、人聲如沸诗良。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,692評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鉴裹。三九已至,卻和暖如春钥弯,著一層夾襖步出監(jiān)牢的瞬間径荔,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,842評論 1 269
  • 我被黑心中介騙來泰國打工脆霎, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留总处,地道東北人。 一個月前我還...
    沈念sama閱讀 47,797評論 2 369
  • 正文 我出身青樓睛蛛,卻偏偏與公主長得像鹦马,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子忆肾,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,654評論 2 354

推薦閱讀更多精彩內(nèi)容