0

The goal is for the program to take user given instagram url and allow to download and save a picture. I've got the main part in place but cant understand how I can go further and use the filtered and right url to download and save the picture on my computer.

My code so far: EDIT: I added a download line but can't seem to get the right file type? I mean it saves as whatever I want it to but I can't open it:

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.')
    



Pelmz
  • 1
  • 1

1 Answers1

0

I think this might help...

import requests
from bs4 import BeautifulSoup as bs
import json
import os.path

insta_url = 'https://www.instagram.com'
inta_username = input('enter username of instagram : ')

response = requests.get(f"{insta_url}/{inta_username}/")

if response.ok:
    html = response.text
    bs_html = bs(html, features="lxml")
    bs_html = bs_html.text
    index = bs_html.find('profile_pic_url_hd')+21
    remaining_text = bs_html[index:]
    remaining_text_index = remaining_text.find('requested_by_viewer')-3
    string_url = remaining_text[:remaining_text_index].replace("\\u0026", "&")

    print(string_url, "\ndownloading...")

while True:
    filename = 'pic_ins.jpg'
    file_exists = os.path.isfile(filename)

    if not file_exists:
        with open(filename, 'wb+') as handle:
            response = requests.get(string_url, stream=True)
            if not response.ok:
                print(response)
            for block in response.iter_content(1024):
                if not block:
                    break
                handle.write(block)
    else:
        continue
    break
print("completed")

You can change the name of the image downloaded by changing the filename variable

Yash Makan
  • 706
  • 1
  • 5
  • 17
  • That doesnt seem to work for me, i get all sorts of errors and as much as i understand, it downloads the profile pic not an actual post – Pelmz Dec 02 '20 at 13:40
  • The same code works on my system and I am using python 3.8. Which version of python you are using ? – Yash Makan Dec 02 '20 at 15:16