保存来自不以图像扩展名结尾的URL的图像



我是python初学者。我有一个包含数千个url的数据集列。我想在每个URL中保存图像及其扩展名。我对以图像扩展名(如https://web.archive.org/web/20170628093753im_/http://politicot.com/wp-content/uploads/2016/12/Sean-Spicer.jpg.(with urllib或请求)结尾的url没有问题。但是,对于link1= https://nypost.com/wp-content/uploads/sites/2/2017/11/171106-texas-shooter-church-index.jpg?quality=90&strip=all&w=1200或link2 = https://i2.wp.com/www.huzlers.com/wp-content/uploads/2017/03/maxresdefault.jpeg?fit=1280%2C720&ssl=1这样的url,我无法保存它们。我想把链接中的图片保存如下:image1.jpg和image2.jpeg。我们该怎么做呢?任何帮助都是有用的。

下面的方法似乎对我有用,试试吧:

import requests
urls = ['https://nypost.com/wp-content/uploads/sites/2/2017/11/171106-texas-shooter-church-index.jpg?quality=90&strip=all&w=1200',
'https://i2.wp.com/www.huzlers.com/wp-content/uploads/2017/03/maxresdefault.jpeg?fit=1280%2C720&ssl=1']
for i, url in enumerate(urls):
r = requests.get(url)
filename = 'image{0}.jpg'.format(i+1)
with open(filename, 'wb') as f:
f.write(r.content)

最新更新