Download Instagram Posts using Python Selenium module

Configurare noua (How To)

Situatie

In this article, we will learn how we can download Instagram posts of a profile using Python Selenium module. Requirements:

  • Google Chrome or Firefox
  • Chrome driver(For Google Chrome) or Gecko driver(For Mozilla Firefox)
  • Selenium package: It is a powerful tool for controlling a web browser through the program. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc. In can installed using the below command:
    pip install selenium

Solutie

Pasi de urmat

Requests package: Requests library is one of an integral part of Python for making HTTP requests to a specified URL. It can be installed using the below command:

pip install requests

# import required modules
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import selenium.common.exceptions
import time
from bs4 import BeautifulSoup as bs
import requests
import os
# get instagram account credentials
username = input(‘Enter Your User Name ‘)
password = input(‘Enter Your Password ‘)
# assign URL
url = ‘https://instagram.com/’ + \
input(‘Enter User Name Of User For Downloading Posts ‘)

Function to start the new session of Browser. You might need to add the path to the web driver. Chrome() function, it depends on your installation.

# get URL path
def path():
global chrome
# starts a new chrome session
# add path if required
chrome = webdriver.Chrome()

Function to enter the URL of the page.

# extract URL
def url_name(url):
# the web page opens up
chrome.get(url)
# webdriver will wait for 4 sec before throwing a
# NoSuchElement exception so that the element
# is detected and not skipped.
time.sleep(4)

Function to enter your login information.

# login to access post
def login(username, your_password):
log_but = chrome.find_element_by_class_name(“L3NKy”)
time.sleep(2)
log_but.click()
time.sleep(4)
# finds the username box
usern = chrome.find_element_by_name(“username”)
# sends the entered username
usern.send_keys(username)
# finds the password box
passw = chrome.find_element_by_name(“password”)
# sends the entered password
passw.send_keys(your_password)
# sends the enter key
passw.send_keys(Keys.RETURN)
time.sleep(5.5)
# Find Not Now Button
notn = chrome.find_element_by_class_name(“yWX7d”)
notn.click()
time.sleep(3)

Function to open the first post.

# function to get first post
def first_post():
pic = chrome.find_element_by_class_name(“kIKUG”).click()
time.sleep(2)

Tip solutie

Permanent

Voteaza

(0 din 0 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?