从谷歌搜索下载图片的工具
gImageGrabber的Python项目详细描述
它提供了通过提取 图片和下载原始图片。
此模块是为64位处理器上的Windows 10编写的。 它使用selenium打开浏览器以便向下滚动以获得比 否则可能。因此,需要浏览器才能正常工作。这是在default 设置为在无法打开浏览器时使用chrome browser将使用firefox。 包装上有Chromedriver和Geckodriver。
安装
要安装万向节抓取器,请执行以下操作:
$ pip install gImageGrabber
有两个python文件imgscrace和imgtools。
imgscrace拥有运行脚本所需的所有实用程序,但是如果您希望对函数有额外的控制权 您可以探索imgtools。
导入
要将此模块导入脚本,请执行以下操作:
fromimggrabberimportimgScrapefromimggrabberimportimgTools
功能
正在生成url
imgScrape.build_url(search)
这是为您的搜索词组成一个google搜索url。
要指定搜索项,请使用函数的search
参数来构建url。
用法:
fromimggrabberimportimgScrapesearchTerm="kamikaze eminem"url=imgScrape.build_url(searchTerm)print(url)#FOR DEBUG PURPOSE
获取源数据
imgScrape.browser(url, test=False)
启动浏览器窗口并向下滚动网页
它返回以utf-8格式编码的网页的原始源代码数据。
它有两个参数url
和test
。
url
是它需要打开的页面的url。test
是为了减少浏览器向下滚动的时间,从而节省时间 返回源代码。这在你写作或 调试脚本中的某些内容。
它使用chrome或firefox来工作,所以要确保你有googlechrome或firefox 安装在默认目录下。
有时,您可能需要单击网页上的show more images以加载更多图像
如果你似乎无法打开浏览器,请确保你在64位操作系统上,并且你有chrome浏览器 或者安装了firefox。
如果你在32位处理器上,你需要使用firefox,你还需要下载32位 来自here的驱动程序,并将其替换为 已经存在保存在python根目录的driver文件夹中的geckodriver.exe。
用法:
fromimggrabberimportimgScrapesearchTerm="kamikaze eminem"url=imgScrape.build_url(searchTerm)raw_data=imgScrape.browser(url)print(raw_data)#FOR DEBUG PURPOSE
提取链接
imgScrape.imageLink(html)
这将从google图像搜索页面的源代码html`(source code) provided.
:code:`html
中提取图像的原始链接。
它返回格式为[链接:文件扩展名]的dict。如果你愿意
在[文件扩展名:link]中,您可以使用imgtools中的imgTools.invDict()`
函数
用法:
fromimggrabberimportimgScrapefromimggrabberimportimgToolssearchTerm="kamikaze eminem"debug=Falseurl=imgScrape.build_url(searchTerm)raw_data=imgScrape.browser(url,debug)links=imgScrape.imageLink(raw_data)print(links)#FOR DEBUG PURPOSEprint(imgTools,invDict(links))#FOR DEBUG PURPOSE
保存图像
imgScrape.saveImages(data, name, onlyType, startingnos, prefix)
这将以一个格式列表保存提供给它的所有图像 [链接:文件扩展名]。
它需要3个参数:
data
这将提供字典,其中包含指向图像的链接,格式为[link:file extension]。name
这是提供将在其中保存图像的文件夹的名称。onlyType
如果您只需要一个特定的文件扩展名,请使用下面的说明 那。如果不是,那么给它一个空字符串或者不使用那个参数。startingnos
如果要从某个数字开始文件命名,请使用 提供该点的参数prefix
您希望在文件名中使用的任何前缀都会出现在这个参数中
它保存图像的格式是
Root folder |-- Search Term |-- file extension(eg 'jpg') |-- 000001.jpg |-- 000002.jpg
用法:
fromimggrabberimportimgScrapesearchTerm="Kamikaze"extension=''#save all types of imagessNos=0#starting nosprefix=""#prefix to file nameurl=imgScrape.build_url(searchTerm)raw_data=imgScrape.browser(url)links=imgScrape.imageLink(raw_data)imgScrape.saveImages(links,searchTerm,extension,sNos,prefix)
示例代码
此代码作为simpleScript.py
包含在包中。
fromimggrabberimportimgScrape# Search termsearch='kamikaze eminem'fType=''# if you want all the files them make it empty stringsNos=0debug=Falseprefix=""html=imgScrape.browser(imgScrape.build_url(search),debug)data=imgScrape.imageLink(html)imgScrape.saveImages(data,search,fType,sNos,prefix)