[英]why couldn't I download images from google with python?
代碼幫助我從谷歌下載了一堆圖片。 幾天前它曾經可以工作,現在突然代碼中斷了。
代碼 :
# importing google_images_download module
from google_images_download import google_images_download
# creating object
response = google_images_download.googleimagesdownload()
search_queries = ['Apple', 'Orange', 'Grapes', 'water melon']
def downloadimages(query):
# keywords is the search query
# format is the image file format
# limit is the number of images to be downloaded
# print urs is to print the image file url
# size is the image size which can
# be specified manually ("large, medium, icon")
# aspect ratio denotes the height width ratio
# of images to download. ("tall, square, wide, panoramic")
arguments = {"keywords": query,
"format": "jpg",
"limit":4,
"print_urls":True,
"size": "medium",
"aspect_ratio": "panoramic"}
try:
response.download(arguments)
# Handling File NotFound Error
except FileNotFoundError:
arguments = {"keywords": query,
"format": "jpg",
"limit":4,
"print_urls":True,
"size": "medium"}
# Providing arguments for the searched query
try:
# Downloading the photos based
# on the given arguments
response.download(arguments)
except:
pass
# Driver Code
for query in search_queries:
downloadimages(query)
print()
輸出日志:
項目編號:1 --> 項目名稱 = Apple 正在評估...開始下載...
不幸的是,由於某些圖像無法下載,因此無法下載所有 4 個。 0 是我們為這個搜索過濾器得到的全部!
錯誤:0
項目編號:1 --> 項目名稱 = 橙色 正在評估...開始下載...
不幸的是,由於某些圖像無法下載,因此無法下載所有 4 個。 0 是我們為這個搜索過濾器得到的全部!
錯誤:0
項目編號:1 --> 項目名稱 = 葡萄 正在評估...開始下載...
不幸的是,由於某些圖像無法下載,因此無法下載所有 4 個。 0 是我們為這個搜索過濾器得到的全部!
錯誤:0
項目編號:1 --> 項目名稱 = 西瓜 正在評估...開始下載...
不幸的是,由於某些圖像無法下載,因此無法下載所有 4 個。 0 是我們為這個搜索過濾器得到的全部!
錯誤:0
這實際上創建了一個文件夾,但其中沒有圖像。
google_images_download項目似乎不再與 Google API 兼容。
作為替代方案,您可以嘗試simple_image_download 。
我認為 Google 正在改變 DOM。 元素 class="rg_meta notranslate" 不再存在。 改為 class="rg_i ..."
def get_soup(url,header):
return BeautifulSoup(urllib2.urlopen(urllib2.Request(url,headers=header)),'html.parser')
def main(args):
query = "typical face"
query = query.split()
query = '+'.join(query)
url = "https://www.google.co.in/search?q="+query+"&source=lnms&tbm=isch"
headers = {}
headers['User-Agent'] = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
soup = get_soup(url, headers)
for a in soup.find_all("img", {"class": "rg_i"}):
wget.download(a.attrs["data-iurl"], a.attrs["data-iid"])
if __name__ == '__main__':
from sys import argv
try:
main(argv)
except KeyboardInterrupt:
pass
sys.exit()
確實這個問題不久前就出現了,Github上已經有一堆類似的問題了:
不幸的是,目前還沒有官方解決方案,您可以使用討論中提供的臨時解決方案。
這不起作用的原因是因為谷歌改變了他們做所有事情的方式,所以你現在需要包含在搜索字符串中的 api_key 。 因此,即使您使用 2.8.0 版本,諸如 google-images-download 之類的軟件包也不再起作用,因為它們沒有占位符來插入 api_key 字符串,您必須向 Google 注冊才能獲得每天 2500 次免費下載。
如果您願意每月支付 50 美元或更多以訪問來自serpapi.com的服務,一種方法是使用 pip 包google-search-results
並提供您的 api_key 作為查詢參數的一部分。
params = {
"engine" : "google",
...
"api_key" : "secret_api_key"
}
您自己提供 API 密鑰,然后調用:
client = GoogleSearchResults(params)
results = client.get_dict()
這將返回一個帶有所有圖像 URL 鏈接的 JSON 字符串,然后您只需直接下載它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.