Search code examples
pythoninstagram

Is possible to get image URL from Instagram URL?


I have an Instagram URL:

https://www.instagram.com/p/B2EtjT9hgvG/

the image inside has that URL:

https://scontent-mxp1-1.cdninstagram.com/vp/cce7a73f8904eea57575a69244b4997b/5DFF22C4/t51.2885-15/sh0.08/e35/s640x640/67472591_2116886595084247_1444361079130496531_n.jpg?_nc_ht=scontent-mxp1-1.cdninstagram.com&_nc_cat=107

if I have only Instagram URL is possible to get image URL with an API or something else?


Solution

  • Thanks to the help of @Chris Doyle I can give you a little help using selenium:

    from selenium import webdriver
    
    driver = webdriver.Chrome('chromedriver.exe')
    driver.get('https://www.instagram.com/p/B2EtjT9hgvG/')
    metas = driver.find_elements_by_tag_name('meta')
    for elem in metas:
        if elem.get_attribute('property') == 'og:image':
            print(elem.get_attribute('content'))
    

    or with requests and bs4 accordingly:

    import requests
    from bs4 import BeautifulSoup
    
    result = requests.get("https://www.instagram.com/p/B2EtjT9hgvG/")
    c = result.content
    soup = BeautifulSoup(c)
    metas = soup.find_all(attrs={"property": "og:image"})
    print(metas[0].attrs['content'])