📅  最后修改于: 2023-12-03 15:39:52.190000             🧑  作者: Mango
如果你需要在你的程序中通过Python搜索谷歌图片,确保使用合法和适当的方式。
Google Images Download是一个可以为任何给定的关键字下载批量图片的命令行程序。它可以快速简单地从Google Images中下载高质量图片。以下是Google ImagesDownload的一些特性:
以下是在Python中使用Google Images Download的示例代码:
!pip install google_images_download
from google_images_download import google_images_download
response = google_images_download.googleimagesdownload()
arguments = {"keywords":"Python", "limit":20, "print_urls":True}
paths = response.download(arguments)
print(paths)
输出结果中包含了下载到的图片的路径:
{'Python': ['/downloads/Python/1. Python_Python logo.png', '/downloads/Python/2. main-qimg-2c6bb26b47ca974ee78e31eced93bbc3.png', '/downloads/Python/3. logo.png']}
如果需要更大范围的搜索图片,可以使用Google Custom Search API。但是,这需要几步步骤来完成。以下是大致的步骤:
在Google Cloud Console中创建API账户和项目,这需要付费。
创建自定义搜索API服务并配置自定义搜索引擎。这涉及到在Google Custom Search Engine网站上创建一个自定义搜索引擎。
使用Python编写程序调用Google Custom Search API进行搜索。这需要安装Google API Python客户端库并使用Google Custom Search Engine ID和API密钥。
以下是使用Python调用Google Custom Search API进行搜索的示例代码:
!pip install google-api-python-client
import urllib.request
import json
from googleapiclient.discovery import build
api_key = 'YOUR_API_KEY'
cse_id = 'YOUR_CSE_ID'
search_query = 'Python'
service = build("customsearch", "v1", developerKey=api_key)
result = service.cse().list(q=search_query, cx=cse_id).execute()
print(result)
输出结果是JSON格式的搜索结果:
{
"kind": "customsearch#search",
"url": {
"type": "application/json",
"template": "https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count}&start={startIndex}&lr={language}&safe={safe}&cx={cx}&cref={cref}&sort={sort}&googlehost={googleHost}&c2coff={disableCnTwTranslation}&hq={hq}&hl={hl}&gl={gl}&gssw={gssw}&yv={yv}&targetLanguage={targetLanguage}&suser={sUser}&callback={callback}"
},
"queries": {
"nextPage": [
{
"title": "Google Custom Search - Python",
"totalResults": "1170000000",
"searchTerms": "Python",
"count": 10,
"startIndex": 11,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"safe": "off",
"cx": "YOUR_CSE_ID"
}
],
"request": [
{
"title": "Google Custom Search - Python",
"totalResults": "1170000000",
"searchTerms": "Python",
"count": 10,
"startIndex": 1,
"inputEncoding": "utf8",
"outputEncoding": "utf8",
"safe": "off",
"cx": "YOUR_CSE_ID"
}
]
},
"context": {
"title": "Google Images"
},
"searchInformation": {
"searchTime": 0.978321,
"formattedSearchTime": "0.98",
"totalResults": "1170000000",
"formattedTotalResults": "1,170,000,000"
},
"items": [
{
"kind": "customsearch#result",
"title": "Python | Python",
"htmlTitle": "\u003cb\u003ePython\u003c/b\u003e | Python",
"link": "https://www.python.org/",
"displayLink": "www.python.org",
"snippet": "The official home of the Python Programming Language.",
"htmlSnippet": "The official home of the \u003cb\u003ePython\u003c/b\u003e Programming Language.",
"cacheId": "ivjhKZBdwE0J",
"formattedUrl": "https://www.python.org/",
"htmlFormattedUrl": "https://www.\u003cb\u003epython\u003c/b\u003e.org/",
"pagemap": {
"metatags": [
{
"viewport": "width=device-width, initial-scale=1",
"apple-mobile-web-app-capable": "yes",
"apple-mobile-web-app-status-bar-style": "black",
"apple-mobile-web-app-title": "Python.org",
"mobile-web-app-capable": "yes",
"theme-color": "#3673a5"
}
]
}
},
{
"kind": "customsearch#result",
...
}
]
}
以上是使用Python搜索谷歌图片的两种方法,你可以根据你的需求来选择最适合你的方式。