例如,我需要让每张图像显示在页面上,同时知道它们的存储地址。我正在寻找的方法将允许使用https://scontent-arn2-1.cdninstagram.com/*
,其中*是一个随机链接,可以列出或下载该链接。
import requests as req
phot = req.get('https://scontent-arn2-1.cdninstagram.com/vp/7987513770b3cfa32372114cde65ff7d/5D53D9CA/t51.2885-15/e35/57306185_137865170666087_417139389797653381_n.jpg?_nc_ht=scontent-arn2-1.cdninstagram.com')
with open('sea.png', 'wb') as f:
f.write(phot.content)
答案 0 :(得分:0)
您可以通过创建一个函数来完成您要描述的内容,该函数采用base_url
和要下载的资源的路径列表:
import requests as req
def get_resources(base_url, resource_paths):
responses = [req.get(base_url + path) for path in resource_paths]
res = get_resource('http://example.com/', ['img1', 'img2', 'etc'])
# This would fetch the following resources:
# - 'http://example.com/img1'
# - 'http://example.com/img2'
# - 'http://example.com/etc'