为了能够使用此库,您需要启用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()中显示它