為了能夠使用此庫,您需要啟用Google自定義搜索API,生成API密鑰憑據並設置一個項目:
訪問https://console.developers.google.com並創建一個項目。
請訪問https://console.developers.google.com/apis/library/customsearch.googleapis.com,並為您的項目啟用“自定義搜索API”。
請訪問https://console.developers.google.com/apis/credentials,並為您的項目生成API密鑰憑據。
請訪問https://cse.google.com/cse/all,在Web表單中創建/編輯自定義搜索引擎啟用“ image Search”選項,並為“搜索網站搜索”選項選擇“選擇”搜索“搜索”整個網絡,但強調包括網站”。
設置Google Developers帳戶和項目後,您應該為您提供開發人員API密鑰和Project CX。
從pypi.org安裝軟件包:
> PIP安裝Google-Images-Search-search
#沒有環境變量:> gimages -k __your_dev_api_key________________your_project_cx__ search -q puppies
#帶有環境變量:>導出gcs_developer_key = __ your_dev_api_key __>導出gcs_cx = __ your_project_cx __ >> gimages search -q puppies
#僅搜索(沒有下載和調整大小):> gimages搜索-Q幼犬
#僅搜索和下載(無調整大小):> gimages搜索-Q幼犬-d/path/on/your/drive/where/drive/where/images/應該/be/download
#搜索,下載和調整大小:> gimages搜索-Q幼犬-D / PATH / -W 500 -H 500
來自Google_images_search導入GoogleImagessearch#您可以使用參數提供API密鑰和CX,#您可以設置環境變量:GCS_DEVELECER_KEY,GCS_CXGIS = GoogleImagessearch('Your_dev_api_key_key','your_project_cx'下面的參考。#對於標記為'##''的參數:# - MultiSelect當前不可行。如果您不希望定義任何值_search_params = {'q':'...':'...','num':10,'filetype':'jpg | gif | gif | png','rights' :'cc_publicdomain | cc_attribute | cc_sharealike | cc_noncmercial | cc_nondederived | cc_nondered','safe':'active | high | high | high | off | off | off | off | off | off | off | off | off | off | off | off | off | off | off | off | off | off | off | off | off | clipart'imgtype':照片|動畫| imgtypeundefined',##'imgsize':'巨大|圖標|大型|中|中|小| xlarge | xxlarge | imgsizeundeundefined',##'imgdominantColor':'黑色|粉紅色|紫色|紅|藍綠色|白|黃色| imgdominantcolorundefined',##'imgcolortype':'顏色|灰色|灰色| trans | imgcolortype tyme | imgcolortypeundefined'##}## )#這將搜索和下載:gis.search(search_params = _search_params,path_to_to_dir ='/path/')#它將搜索,下載和調整: = 500,高度= 500)#首先搜索,然後下載並調整大小:gis.search(search_params = _search_params)for gis.results中的image():image.URL#image direct urlimage.referrer_url_url# image.download('/path/')#下載imageImage .resize(500,500)#調整下載的imageImage.path#下載本地文件路徑
有時,您需要保存帶有您選擇的文件名的圖像。
來自Google_images_search Import import GoogleImagessearchgis = GoogleImagessearch('your_dev_api_key','your_project_cx')_ search_params = {...} custom_image_name ='my_image')
Google的API限制是每個請求的10張圖像。
這意味著,如果您想要123張圖像,它將內部分為13個請求。
請記住,如果啟用圖像驗證,獲得123張圖像將需要更多時間。
來自Google_images_search import import googleImagessearchgis = googleImagessearch('your_dev_api_key','your_project_cx')_ search_params = {'q'q':'...'...'...'...','num':123, }#獲取第一個123張圖像:gis.search(search_params = _search_params)#從Google圖像中獲取下一個123個圖像搜索:gis.next_page()用於gis.results中的圖像(): ...
默認情況下,每個圖像URL將被驗證。
這意味著如果可以獲取和驗證標題,則將檢查每個圖像URL。
因此,您不需要警惕哪個圖像URL實際上可以下載。
缺點是驗證所需的時間。
如果願意,可以將其關閉。
來自Google_images_search import GoogleImagessearch#使用“ validate_images”關閉驗證,agrumentgis = googleimagessearch('your_dev_api_key','your_project_cx',validate_images = false)
默認情況下,未啟用progressbar。
默認情況下,僅在CLI Progressbar中使用Curses庫啟用。
在程序模式下,可以通過兩種方式啟用它:
使用上下文模式(詛咒)
使用您的自定義progressbar函數
來自Google_images_search import import GoogleImagessearch#使用您的自定義progressbar functionDef my_progressbar(url,progress):print(url +' +' +' + progress +'%')gis = googleimagessearch('your_dev_api_ekey','your_dev_api_key','your_project_cx ',your_project_cx',reprogresbar_fn = my_parramseachbare = my_spareRapparbar) 。 ...
來自google_images_search import import googleimagessearch from io import bytesiofrom pil導入圖像#在這種情況下,在這種情況下,我們使用pil將字節作為圖像對象#unde ume uncoge#undies#我們不必等待磁盤保存/寫入時間#圖像#image#簡單地保存在中內存#本示例應顯示3張小狗的圖片!gis = googleimagessearch('ur_dev_api_key','your_project_cx')my_bytes_io = bytesio = bytesio()在gis.results()中:#在這裡,我們告訴字節對象返回地址為0my_bytes_io.seek(0)#取原始image dataraw_image_data = image.get_raw_data()#此函數將原始image數據寫入objectImage.copy_to(copy_to)(copy_to(copy_to)( my_bytes_io, raw_image_data)#或不使用RAW數據,該數據將在copy_to()methodimage.copy.to(my_bytes_io)中自動獲取##創建一個臨時映像ObjectTemp_img = image.open(my_bytes_io)#在默認系統photo viewertemp_img.show( )中顯示它