【问题标题】:How can I save Instagram stories in my media folder using media url?如何使用媒体 URL 将 Instagram 故事保存在我的媒体文件夹中?
【发布时间】:2021-08-13 17:40:16
【问题描述】:
def save_media(story_id, media_url):
try:
    link = media_url[:media_url.find('?')]
    extension = link[::-1][:link[::-1].find('.')+1][::-1]
    if 'video' in media_url:
        extension = '.mp4'
        
    filepath = r'{0}\file path for media to save stories\{1}{2}'.format(os.getcwd(), story_id, extension)
    if not os.path.exists(filepath):
        response = requests.get(media_url)
        if response.status_code==200:
            with open(r'{}'.format(filepath), 'wb') as file:
                file.write(response.content)
            file.close()
    newpath = filepath.replace(f'{os.getcwd()}\\influnite', '')
    return newpath
except Exception as error:
    print('Error saving story media!')
    print(error)
    return ''

运行此代码后从 api 获取媒体 url 我没有在媒体文件夹中获取媒体数据(视频和故事) 有人能告诉我我在做什么错吗?

【问题讨论】:

  • 可以,但是需要使用selenium而不是纯python。记住 instagram 使用 javascript 来加载它的内容。
  • @EddwinPaz 其实我试过这个是用来下载照片的,但我需要下载故事和视频
  • 我做到了

标签: django django-rest-framework django-media instagram-story


【解决方案1】:
def save_media(story_id, media_url):
try:
    link = media_url[:media_url.find('?')]
    extension = link[::-1][:link[::-1].find('.')+1][::-1]
    if 'video' in media_url:
        extension = '.mp4'
        
    filepath = r'{0}/influnite/media/stories/{1}{2}'.format(os.getcwd(), story_id, extension)
    if not os.path.exists(filepath):
        response = requests.get(media_url)
        if response.status_code==200:
            with open(filepath, 'wb') as file:
                file.write(response.content)
            file.close()
    newpath = filepath.replace(r'{0}/influnite'.format(os.getcwd()), '')
    return newpath
except Exception as error:
    print('Error saving story media!')
    print(error)
    return ''

这里首先检查获取该故事是jpg格式或mp4,然后检查来自服务器的响应并将故事和文件保存为wb,即写入位并将故事保存在@ 987654325@文件夹

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2021-10-09
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多