Day 14 : Web Scraping using python (Instagram Image downloader)

Hey guys,

Previously we learn web scraping using python for static websites.

Today we will learn about web scraping using python for dynamic websites.
To automate process like downloading images, placing an order or anything on the web can be done by web scraping.

We require libraries like selenium and beautifulsoup

Selenium: 


Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. Through Selenium Python API you can access all functionalities of Selenium WebDriver in an intuitive way.
Selenium Python bindings provide a convenient API to access Selenium WebDrivers like Firefox, Ie, Chrome, Remote etc. The current supported Python versions are 2.7, 3.5 and above.
This documentation explains Selenium 2 WebDriver API. Selenium 1 / Selenium RC API is not covered here.

How to install?
pip install selenium

Instagram Image Downloader

Read Readmd file

from selenium import webdriver
Import webdriver from selenium, this used open browser depends on browser.

webdriver = webdriver.Chrome()
This will open chrome web driver.(open chrome browser)

from time import sleep
sleep(2)
Import sleep function from the time module, sleep delay the working.

webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
This will open a specified URL in the browser.

username = webdriver.find_element_by_name('username')
As the name suggest, find element by name , we know websites are in html so this function find that element with specified name in full website.

username.send_keys('username')
Fill that text filed with a specified username  (depends on your username on instagram) 

button_login = webdriver.find_element_by_class_name("y3zKF")
As the name suggest, find element by class name , we know websites are in html so this function find that element with specified class name in full website.

button_login.click()
Click on button.

soup = BeautifulSoup(webdriver.page_source, 'html.parser')
Yesterday we learn about Beautifulsoup, same here we pass our page source and convert to html.

allimages = soup.select('img')
this will select all image tags. or in other words, you can say all images.

img_data = rq.get(img_link).content
As we have img_link , request module is used to GET that image link and content(i.e image).

with open("insta/" + str(index + 1) + ".jpg", 'wb+') as f:
f.write(img_data)
basically here we save our images, we know images are in jpg or jpeg or png format and we have save these file with those extension at write location.
wb+ : as images are in binary format.


That's it you get whatever images you want.
just remember one thing, you have to follow that person or that personal profile is public.



❤❤Quarantine python group link ❤❤

8805271377 WhatsApp

Follow here ❤

@mr._mephisto_ Instagram 

There will be no restrictions just feel free to learn. 

Share and take one more step to share knowledge to others. 

Believe in yourself 🤟 you are awesome. 

Be safe, Be happy😁
Take care of yourself and your family 
Of course watch movies and series🤟😉 

And follow the guidelines of government

Comments

Popular posts from this blog

Day 16 : Pandas Basics

News website using Flask and news API - Working with templates : Part 5

Day 9 : Encapsulation and polymorphism