同意Blairg23的观点,使用urllib.request.urlretrieve
是最简单的解决方案之一。
我想在这里指出一个注意事项。有时它不会下载任何东西,因为请求是通过脚本(bot)发送的,如果你想从谷歌图像或其他搜索引擎解析图像,你需要先传递user-agent
给请求headers
,然后再下载图像,否则,请求将被阻止并抛出错误。
通过user-agent
并下载图像:
opener=urllib.request.build_opener()
opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582')]
urllib.request.install_opener(opener)
urllib.request.urlretrieve(URL, 'image_name.jpg')
在线 IDE 中的代码,使用requests
, bs4
, urllib.requests
.从 Google 图片中抓取和下载图片
或者,如果您的目标是从 Google、Bing、Yahoo!、DuckDuckGo(和其他搜索引擎)等搜索引擎抓取图像,那么您可以使用SerpApi。这是一个带有免费计划的付费 API。
最大的不同是无需弄清楚如何绕过搜索引擎的阻止或如何从 HTML 或 JavaScript 中提取某些部分,因为它已经为最终用户完成了。
要集成的示例代码:
import os, urllib.request
from serpapi import GoogleSearch
params = {
"api_key": os.getenv("API_KEY"),
"engine": "google",
"q": "pexels cat",
"tbm": "isch"
}
search = GoogleSearch(params)
results = search.get_dict()
print(json.dumps(results['images_results'], indent=2, ensure_ascii=False))
# download images
for index, image in enumerate(results['images_results']):
# print(f'Downloading {index} image...')
opener=urllib.request.build_opener()
opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582')]
urllib.request.install_opener(opener)
# saves original res image to the SerpApi_Images folder and add index to the end of file name
urllib.request.urlretrieve(image['original'], f'SerpApi_Images/original_size_img_{index}.jpg')
-----------
'''
]
# other images
{
"position": 100, # 100 image
"thumbnail": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQK62dIkDjNCvEgmGU6GGFZcpVWwX-p3FsYSg&usqp=CAU",
"source": "homewardboundnj.org",
"title": "pexels-helena-lopes-1931367 - Homeward Bound Pet Adoption Center",
"link": "https://homewardboundnj.org/upcoming-event/black-cat-appreciation-day/pexels-helena-lopes-1931367/",
"original": "https://homewardboundnj.org/wp-content/uploads/2020/07/pexels-helena-lopes-1931367.jpg",
"is_product": false
}
]
'''
免责声明,我为 SerpApi 工作。