I am trying to scrape images from Instagram using python. In order to do that, I have written a small piece of code. Here is the code:
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as req
url = "https://www.instagram.com/p/CE9CZmsghan/"
website = req(url)
pg = website.read()
website.close()
pgsoup = soup(pg,"html.parser")
print(pgsoup.findAll('div',{'class' : 'KL4Bh'}))
I have just written the code until converting the html into a soup object. Strangely, nothing is getting printing in the last line. Only an empty list is getting printed. Do you know why? And do you know how to fix it?
Its simple with usage of selenium
:
from selenium import webdriver
import os
chrome_driver = os.path.abspath(os.path.dirname(__file__)) + '/chromedriver'
browser = webdriver.Chrome(chrome_driver)
url = 'https://www.instagram.com/p/CE9CZmsghan/'
browser.get(url)
image_url = browser.find_element_by_class_name('KL4Bh').find_element_by_tag_name('img').get_attribute('src')
Output:
https://scontent-frx5-1.cdninstagram.com/v/t51.2885-15/e35/s1080x1080/119122193_326279868428134_4046851753042951785_n.jpg?_nc_ht=scontent-frx5-1.cdninstagram.com&_nc_cat=1&_nc_ohc=MU53uPwIAzoAX-OPPES&_nc_tp=15&oh=7e874789a4624589c92c9c4f5e030387&oe=5F8F3371