用于从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选项来避免使用受版权保护的材料。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
泛型Java将参数约束到公共超类   java Spring引导:在构造函数中加载属性文件并用作autowire注释   java中的优先级队列顺序错误。util。优先级队列和特定比较器   带有Java Sprint引导REST的Google应用程序引擎标准在GCLOUD服务器中不起作用   安卓从Java代码中检索变量并将其作为参数分配给TestNG   用于读取列表值的Java JSON对象   java Hibernate映射:实体映射中的重复列   多线程。start()不从Java中的父线程分派   java Android facebook webdialog网络错误(netstack:lib_mgr错误)   http使用Java阻止网站   java DynamicAsper:访问连接报表中动态列的值   java如何分离文件中的每个单词,并在表中显示每个单词和每个单词的编号?   如何打包和部署EclipseJava应用程序?   java使用Mule Anypoint,我想实现没有flowref的功能   java Kafka consumer ClassNotFoundException   java错误捕获帮助;消息不断重复   javaspring,Thymeleaf和CSS如何给错误着色   javascript如何在java中实现反向ajax   如何通过UDP连接从java数据包中读取序列号?