このライブラリを使用できるようにするには、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フォームにアクセスして、カスタム検索エンジンを作成/編集します。含まれるサイト」。
Google Developersアカウントとプロジェクトを設定した後、開発者APIキーとプロジェクトCXが提供されるはずです。
pypi.orgからパッケージをインストールする:
> PIPインストールGoogle-Images-Search
#環境変数なし:> gimages -k __your_dev_api_key__ -c __your_project_cx__検索-q子犬
#環境変数:> export gcs_developer_key = __ your_dev_api_key __> export gcs_cx = __ your_project_cx __ >> gimages search -q puppies
#検索のみ(ダウンロードとサイズ変更なし):>ギメージ検索-Q子犬
#検索とダウンロードのみ(サイズ変更なし):>ギメージ検索-Q子犬-D/パス/on/your/drive/where/images/sef/be/downloaded
#検索、ダウンロード、およびサイズを変更:>ギメージ検索-Q子犬-D / PATH / -W 500 -H 500
Google_images_searchのインポートからgoogleimagessearch#googlemagessearch#を提供できます。#または環境変数を設定することができます:gcs_developer_key = googleimagessearch( 'your_dev_api_key'、 'your_project_cx')参照を簡単に参照してください。#「##」でマークされたパラメーションの場合:# - マルチセレクトは現在実行不可能です。 1つのオプションのみを選択してください#-このparamは、value_search_params = {'q': '...'、 'num':10、 'filetype': 'jpg | gif | png '、' rights ':' cc_publicdomain | cc_attribute | cc_sharealike | cc_noncommercial | cc_nonderived '、' safe ':' active | high | off | safeundefined '、##' imgtype ':' clipart | clipart | stock |写真|アニメーション| imgtypeundefined '、##' imgsize ':'巨大|アイコン|大規模|小| xlarge | xxlarge | imgsizeUndefined '、##' imgdominantcolor ':' black | blue |茶色|灰色|緑|オレンジ|ピンク|紫|赤|ティール|白|黄色| imgdominantcolorundefined '、##' imgcolortype ':' color | gray | mono | trans | imgcolortypeundined '##} ) = 500、height = 500)#最初に検索してから、その後ダウンロードしてサイズを変更します:gis.search(search_params = _search_params)for gis.results():image.url#image direct urlimage.referrer_url#imageリファラー(ソース) image.download( '/path/')#download imageimage.resize(500、500)
選択したファイル名で画像を保存したい場合があります。
Google_images_searchからインポートGoogleimagessearchgis = googleimagessearch( 'your_dev_api_key'、 'your_project_cx')_ search_params = {...} gis.search(search_params = _search_params、path_to_dir = '...'、 custom_image_name = 'my_image')
GoogleのAPI制限は、リクエストごとに10画像です。
つまり、123の画像が必要な場合は、13のリクエストに内部的に分割されます。
画像の検証が有効になっている場合、123の画像を取得するにはもう少し時間がかかることに注意してください。
Google_images_searchからインポートGoogleimagessearchGis = googleimagessearch( 'your_dev_api_key'、 'your_project_cx')_ search_params = {'q': '...'、 'num':123、 }#最初の123画像を取得する:gis.search(search_params = _search_params) ...
すべての画像URLはデフォルトで検証されます。
つまり、ヘッダーをフェッチして検証できる場合、すべての画像URLがチェックされます。
そのため、どの画像URLが実際にダウンロード可能かどうかを警戒する必要はありません。
欠点は、検証するのに必要な時間です。
必要に応じて、オフにすることができます。
Google_images_searchのインポートからgoogleimagessearch# "validate_images" agrumentgis = googleimagessearch( 'your_dev_api_key'、 'your_project_cx'、validate_images = false)で検証をオフにします)
デフォルトでは、ProgressBarは有効になりません。
CLI ProgressBarでのみ、デフォルトでCurses Libraryを使用して有効になります。
プログラマティックモードでは、2つの方法で有効にできます。
コンテキストモード(呪い)の使用
カスタムProgressBar関数を使用します
Google_images_searchからインポートGoogleimagessearch#カスタムProgressbar functiondef my_progressbar(url、progress):print(url + '' + progress + '%')gis = googleimagessearch( 'your_dev_api_key'、 'your_project_cx'、progressbar_fn = my_progressbar) ..} gis.search(search_params = _search_params)#googleimagessearch( 'your_dev_api_key'、 'your_project_cx')を使用してコンテキストモード(curses)を使用:_search_params = {...} gis.search(search_search_search_search_params) ...
Google_images_searchからImport Import googleimagessearch from io Import bytesiofrom pil import image#この場合、PILを使用して画像オブジェクトとしてbytesioを維持します#ディスクの保存 /書き込み時間を待つ必要はありません。メモリ#この例は、子犬の3枚の写真を表示する必要があります!gis = googleimagessearch( 'your_dev_api_key'、 'your_project_cx')my_bytes_io = bytesio()gis.search({'q': 'puppies'、 'num':3}) gis.results():#ここでは、bytesioオブジェクトに0my_bytes_io.seek(0)に戻るように戻すように指示します#dataaraw_image_data = image.get_raw_data() my_bytes_io、raw_image_data)#またはcopy_to()methodimage.copy_to(my_bytes_io)内で自動的に#copy_to(my_bytes_io)内で#を自動的に取得しない生データなしで、再びfinignmy_bytes_io.seek(0)#を読むことができます。一時的なイメージを作成するObjectTemp_img = image.open(my_bytes_io)#デフォルトのシステムに表示されますphotoertemp_img.show()