用于从ecosia搜索和下载图像的python模块
ecosia-images的Python项目详细描述
python ecosia图像
用于从ecosia搜索和下载图像的python模块
安装
pip install ecosia-images
设置
使用库的唯一必要条件是安装web浏览器及其驱动程序目前,该软件包可以与google chrome或firefox一起使用。
如果使用google chrome,则还需要安装chromedriver并在path中可访问。有关详细信息,请参见following link
至于firefox,geckodriver需要在path中安装并可访问。
示例
启动爬虫程序
>>>fromecosia_imagesimportcrawler>>>searcher=crawler()
要使用的浏览器可以传递给爬网程序构造函数
>>>fromecosia_imagesimportcrawler>>>searcher=crawler(browser='firefox')
要查看所有有效的浏览器选项,请参见ecosia_images.browser_options
。
>>>fromecosia_imagesimportbrowser_options>>>browser_options['chrome','firefox']
搜索图像并获取图片链接
声明爬网程序并使用它搜索关键字后,生成的链接将可由links
属性访问
>>>searcher=crawler()>>>searcher.search('number 9')>>>searcher.links{...}# urls
使用选项搜索
搜索还可以包括ecosia为优化结果提供的选项。用于优化搜索的可用键和值存储在ecosia_images.download_options
中。
>>>fromecosia_imagesimportdownload_options>>>download_options{'size':['small','medium','large','wallpaper'],'color':['colorOnly','monochrome','red','orange','yellow','green','teal','blue','purple','pink','brown','black','gray'],'image_type':['photo','clipart','line','animated'],'freshness':['day','week','month'],'license':['share','shareCommercially','modify','modifyCommercially','public']}
当调用爬网程序的search
方法时,可以指定选定的选项。
>>>searcher.search('trees',color='monochrome',size='wallpaper')>>>searcher.links{...}# links to big pictures of trees in black and white
收集更多链接
如果需要更多的链接,可以使用函数gather_more
>>>searcher.search('bees')>>>len(searcher.links)50# Give or take>>>searcher.gather_more()>>>len(searcher.links)100# Give or take
下载图像
在以下所有情况下,脚本首先检查图像是否已下载,以便不再下载函数将返回下载图像的文件路径。
函数download
将下载给定数量的图片,并将它们保存在名称与关键字一致的文件夹中。此文件夹将在调用构造函数时指定的文件夹内创建。在下面的示例中,图像将保存在/path/to/folder/keyword/中。
>>>searcher=crawler(directory='/path/to/folder/')>>>searcher.search('keyword')>>>searcher.download(100)[...]# list with file paths
如果未指定文件夹,则图像将保存在当前工作目录中名为downloads的新文件夹中。
还有一个download_all
函数,它将下载crawler对象中当前可用的所有链接
>>>searcher.search('pidgeons')>>>searcher.download_all()[...]
停止客户
必须停止爬虫程序以避免资源泄漏。
>>>searcher.stop()
文件名
用于下载文件的命名约定可以传递给爬网程序构造函数。要查看所有有效的命名选项,请参见ecosia_images.naming_options
。
>>>fromecosia_imagesimportcrawler,naming_options>>>searcher=crawler(naming='hash')>>>naming_options['trim','hash']
自定义命名
对于特定的命名约定,可以将函数传递给构造函数。如果计划重命名文件,请确保使用此功能,因为重命名文件会干扰爬网程序避免下载重复文件的能力
函数必须采用三个参数:url、directory和keyword。
如果计划不使用库提供的默认文件夹,请禁用此选项,这样爬网程序就不会创建目录
>>>defcustom_naming(url,directory,keyword):...# Function implementation...returnfilename>>>searcher=crawler(naming_function=custom_naming,makedirs=False)
免责声明
下载的图片来自Ecosia搜索引擎,它们可能拥有版权。请勿下载或使用任何违反其版权条款的图像。您可以利用search
函数的license
选项来避免使用受版权保护的材料。