保存 URL 中不以图像扩展名结尾的图像
Saving an image from a URL that does not end with image extension
我是 python 初学者。我有一个包含数千个 URL 的数据集列。我想将图像及其扩展名保存在每个 URL 中。我对以图像扩展名结尾的网址没有问题,例如 https://web.archive.org/web/20170628093753im_/http://politicot.com/wp-content/uploads/2016/12/Sean-Spicer.jpg.(使用 urllib 或请求)
但是对于像 link1= https://nypost.com/wp-content/uploads/sites/2/2017/11/171106-texas-shooter-church-index.jpg?quality=90&strip=all&w=1200 or 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)
我是 python 初学者。我有一个包含数千个 URL 的数据集列。我想将图像及其扩展名保存在每个 URL 中。我对以图像扩展名结尾的网址没有问题,例如 https://web.archive.org/web/20170628093753im_/http://politicot.com/wp-content/uploads/2016/12/Sean-Spicer.jpg.(使用 urllib 或请求) 但是对于像 link1= https://nypost.com/wp-content/uploads/sites/2/2017/11/171106-texas-shooter-church-index.jpg?quality=90&strip=all&w=1200 or 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)