Created
September 30, 2020 12:11
-
-
Save HarshitRuwali/2e47e3182baa8aef58b34332a90f7fcf to your computer and use it in GitHub Desktop.
Download the instagram videos and images easily.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
from datetime import datetime | |
import requests | |
from tqdm import tqdm | |
import re | |
def download(): | |
url = input("Please enter the URL: ") | |
x = re.match(r'^(https:)[/][/]www.([^/]+[.])*instagram.com', url) | |
if x: | |
request_image = requests.get(url) | |
src = request_image.content.decode('utf-8') | |
check_type = re.search(r'<meta name="medium" content=[\'"]?([^\'" >]+)', src) | |
check_type_f = check_type.group() | |
final = re.sub('<meta name="medium" content="', '', check_type_f) | |
if final == "image": | |
print("\nDownloading the image...") | |
extract_image_link = re.search(r'meta property="og:image" content=[\'"]?([^\'" >]+)', src) | |
image_link = extract_image_link.group() | |
final = re.sub('meta property="og:image" content="', '', image_link) | |
_response = requests.get(final).content | |
file_size_request = requests.get(final, stream=True) | |
file_size = int(file_size_request.headers['Content-Length']) | |
block_size = 1024 | |
filename = datetime.strftime(datetime.now(), '%Y-%m-%d-%H-%M-%S') | |
t=tqdm(total=file_size, unit='B', unit_scale=True, desc=filename, ascii=True) | |
with open(filename + '.jpg', 'wb') as f: | |
for data in file_size_request.iter_content(block_size): | |
t.update(len(data)) | |
f.write(data) | |
t.close() | |
print("Image downloaded successfully") | |
if final == 'video': | |
print('downloading') | |
extract_video_link = re.search(r'meta property="og:video" content=[\'"]?([^\'" >]+)', src) | |
video_link = extract_video_link.group() | |
final = re.sub('meta property="og:video" content="', '', video_link) | |
_response = requests.get(final).content | |
file_size_request = requests.get(final, stream=True) | |
file_size = int(file_size_request.headers['Content-Length']) | |
block_size = 1024 | |
filename = datetime.strftime(datetime.now(), '%Y-%m-%d-%H-%M-%S') | |
t=tqdm(total=file_size, unit='B', unit_scale=True, desc=filename, ascii=True) | |
with open(filename + '.mp4', 'wb') as f: | |
for data in file_size_request.iter_content(block_size): | |
t.update(len(data)) | |
f.write(data) | |
t.close() | |
print("Video downloaded successfully") | |
while True: | |
try: | |
download() | |
sys.exit() | |
except (KeyboardInterrupt): | |
print("Programme Interrupted") |
I cannot say why you are getting this error. Everything is working fine on my end.
And btw there is a tool called InstaPy. Use that it will reduce you effort of getting every single link for images and stuff as you seems like to be downloading all the images from a single page.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Profile picture Downloading...
handy_hamper: 100%|###############################################################################################| 12.9k/12.9k [00:00<00:00, 800kB/s]
link loop started
loop link <a href= : https://www.instagram.com/p/CH2I7l5DOO_/
Unknown URL
loop link <a href= : https://www.instagram.com/p/CHEh77gjdWx/
Unknown URL
loop link <a href= : https://www.instagram.com/p/CHAK7wJnEzs/
Unknown URL
loop link <a href= : https://www.instagram.com/p/CHADKpeHY3I/
Unknown URL