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