【发布时间】:2021-03-14 10:19:31
【问题描述】:
程序的目标是获取用户给定的 instagram url 并允许下载和保存图片。 我已经完成了主要部分,但无法理解如何进一步使用过滤后的正确 url 下载图片并将其保存在我的计算机上。
到目前为止我的代码: 编辑:我添加了一个下载行,但似乎无法获得正确的文件类型?我的意思是它可以保存为我想要的任何内容,但我无法打开它:
import requests
import re
import shutil
def get_response(url):
r = requests.get(url)
while r.status_code != 200:
r.raw.decode_content = True
r = requests.get(url, stream = True)
return r.text
def prepare_urls(matches):
return list({match.replace("\\u0026", "&") for match in matches})
url = input('Enter Instagram URL: ')
response = get_response(url)
vid_matches = re.findall('"video_url":"([^"]+)"', response)
pic_matches = re.findall('"display_url":"([^"]+)"', response)
vid_urls = prepare_urls(vid_matches)
pic_urls = prepare_urls(pic_matches)
if vid_urls:
print('Detected Videos:\n{0}'.format('\n'.join(vid_urls)))
print("Can't download video, the provided URL must be of a picture.")
if pic_urls:
print('Detected Pictures:\n{0}'.format('\n'.join(pic_urls)))
from urllib.request import urlretrieve
dst = 'Instagram picture.jpg'
urlretrieve(url, dst)
#EDIT ^
if not (vid_urls or pic_urls):
print('Could not recognize the media in the provided URL.')
【问题讨论】:
标签: python python-3.x download