繁体   English   中英

Python:从Google下载图片

[英]Python: Download Images from Google

我正在使用Google的Books API根据ISBN号获取有关图书的信息。 我正在获取缩略图以及其他信息。 响应如下所示:

 "imageLinks": {
 "smallThumbnail": "http://books.google.com/books/content?id=tEDhAAAAMAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api",
 "thumbnail": "http://books.google.com/books/content?id=tEDhAAAAMAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api"
},

我想在上面的链接上下载缩略图,并将其存储在本地文件系统中。 python怎么做?

使用urllib模块

例如:

import urllib

d = {"imageLinks": {
     "smallThumbnail": "http://books.google.com/books/content?id=tEDhAAAAMAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api",
     "thumbnail": "http://books.google.com/books/content?id=tEDhAAAAMAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api"
     }
}

urllib.urlretrieve(d["imageLinks"]["thumbnail"], "MyThumbNail.jpg")

Python3X

from urllib import request

with open("MyThumbNail.jpg", "wb") as infile:
    infile.write(request.urlopen(d["imageLinks"]["thumbnail"]).read())

将HTTP请求发送到相应的URL,然后获取content 然后将内容写入文件。

看例子

imageLinks = {
    "smallThumbnail": "http://books.google.com/books/content?id=tEDhAAAAMAAJ&printsec=frontcover&img=1&zoom=5&source=gbs_api",
    "thumbnail": "http://books.google.com/books/content?id=tEDhAAAAMAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api"
}
import requests

img = requests.get(imageLinks['thumbnail']).content
with open("myfile.jpg", 'wb') as img_file:
    img_file.write(img)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM