Ron*_*nfe 0 python stream http-headers python-3.x python-requests
嘿,我刚刚做了一些研究,发现我可以从以filename.extension结尾的 url 下载图像,例如000000.jpeg。我现在想知道如何下载没有任何扩展名的图片。这是我想下载图片的网址 http://books.google.com/books/content?id=i2xKGwAACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api 当我将网址直接放入浏览器时,它会显示一张图片
此外,这是我尝试过的:
from six.moves import urllib
thumbnail='http://books.google.com/books/content?id=i2xKGwAACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api'
img=urllib.request.Request(thumbnail)
pic=urllib.request.urlopen(img)
pic=urllib.request.urlopen(img).read()
Run Code Online (Sandbox Code Playgroud)
Anyhelp将不胜感激
这是一种使用 HTTP 响应标头执行此操作的方法:
import requests
import time
r = requests.get("http://books.google.com/books/content?id=i2xKGwAACAAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api", stream=True)
ext = r.headers['content-type'].split('/')[-1] # converts response headers mime type to an extension (may not work with everything)
with open("%s.%s" % (time.time(), ext), 'wb') as f: # open the file to write as binary - replace 'wb' with 'w' for text files
for chunk in r.iter_content(1024): # iterate on stream using 1KB packets
f.write(chunk) # write the file
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1583 次 |
| 最近记录: |