0

I have been trying to download some imagesfrom Twitter which worked pretty well. Now I am trying to download some images from Instagram like the one below: But my code is failing and I do not know why. Could anybody please help me?

Here is my code:

import selenium.webdriver as webdriver
import requests
import time

driver = webdriver.Chrome()
driver.get('https://www.instagram.com/p/BQEDSdUBcik/')
time.sleep(10) 
iframe_counter = 0

while True:
    try:
        driver.switch_to_frame(iframe_counter)
        pictures = driver.find_elements_by_xpath('//window._sharedData')
        print(pictures)
        if len(pictures) > 0:
            for pic in pictures:
                response = requests.get(pic.get_attribute('display_src')).content
                with open('C:\\Instagram\\%s.jpeg' % (str(iframe_counter) + str(pictures.index(pic))), 'wb') as f:
                    f.write(response)
                    driver.switch_to_default_content()
                    hexframe_counter += 1
    except WebDriverException:
        break

driver.close()
driver.quit()

Many thanks and regards, Andi

ᴀʀᴍᴀɴ
  • 4,443
  • 8
  • 37
  • 57
Andi Maier
  • 914
  • 3
  • 9
  • 28
  • What is the exact problem? – ᴀʀᴍᴀɴ Feb 13 '17 at 20:47
  • @Andi Maier, this code works for getting images from `iframes` only. You can combine your code from http://stackoverflow.com/questions/42133073/images-download-with-beautifulsoup to make it work both for page source and `iframes`... also note that there is `iframe_counter` variable defined in the begining while you're trying to increase `hexframe_counter` on each iteration... – Andersson Feb 13 '17 at 20:59
  • But for some reason there is nothing downloaded from the Instagram page... – Andi Maier Feb 14 '17 at 10:37

0 Answers0