Automating Instagram

with Python and Selenium

Tim Großmann

@timigrossmann

Passionate learner and developer. Studying CS at the Stuttgart Media University.

 

Creator of InstaPy

/timgrossmann

@TimGrossmann

contact.timgrossmann

Why Automation?

Why Automation?

- Repetitive task are done by hand

Why Automation?

- Repetitive task are done by hand

- One time effort

Why Automation?

- Repetitive task are done by hand

- One time effort

- Easily automatable

Selenium

automating browsers

Selenium

automating browsers

- Testing web apps

Selenium

automating browsers

- Testing web apps
- Automating boring tasks

Selenium

automating browsers

- Testing web apps
- Automating boring tasks

- Dynamically rendered content

Selenium

automating browsers

- Testing web apps
- Automating boring tasks

- Dynamically rendered content

        pip install selenium

Starting a session

from time import sleep
from selenium import webdriver

# starting a new browser session
browser = webdriver.Chrome('./assets/chromedriver')

# navigating to a webpage
browser.get('https://www.instagram.com') 

# make sure the browser stays open for 5sec
sleep(5)

# clean exit
browser.close()

Finding Elements

Finding/Clicking Elements

# find Log in link
login_elem = browser.find_element_by_xpath(
    '//article/div/div/p/a[text()="Log in"]')

# click it
login_elem.click()

ActionChains

ActionChains

# find form inputs and enter data
inputs = browser.find_elements_by_xpath(
    '//form/div/input')

ActionChains(browser)\
    .move_to_element(inputs[0]).click()\
    .send_keys('contacting.john.doe')\
    .move_to_element(inputs[1]).click()\
    .send_keys('passtheword')\
    .perform()

# find Log in button and click it
login_button = browser.find_element_by_xpath(
    '//form/span/button[text()="Log in"]')

ActionChains(browser)\
    .move_to_element(login_button)\
    .click().perform()

Special Keys

Special Keys

from selenium.webdriver.common.keys import Keys

# find the body element to send keys to
body_elem = browser.find_element_by_tag_name('body')

# move up and down to load new images
for _ in range(3):
    body_elem.send_keys(Keys.END)
    sleep(2)
    body_elem.send_keys(Keys.HOME)
    sleep(2)   

Automated Liking

Automated Liking

# find all the heart links
hearts = browser.find_elements_by_xpath(
    '//a[contains(@class, "_tk4ba _1tv0k")]')

# click all the heart links
for i, heart in enumerate(hearts):
    print('{}/{}'.format(i, len(hearts)))
    heart.click()
    sleep(2)

Problems

When working with Frontend

Problems

When working with Frontend

- Changing page layout

Problems

When working with Frontend

- Changing page layout
- Different languages

Problems

When working with Frontend

- Changing page layout
- Different languages

- A/B Testing

Page Object

Design Pattern

Page Object

Design Pattern

Page Object

Design Pattern

- Maintainabiliyt 

- Reduced duplicated code

Page Object

Design Pattern

- Maintainabiliyt 

- Reduced duplicated code

- Nice API Layer

Page Object

Page Object

class InstaLoginPage:
    def __init__(self, browser):
    """init all the wrapper here"""
        self.browser = browser
        self.userbox = browser.find_element_by_xpath('...')
        self.pw_box = browser.find_element_by_xpath('...')
        self.login_button = browser.find_element_by_xpath('...')

    def login(self, username, password):
    """Login user with username and pw"""
        ActionChains(self.browser)\
            .move_to_element(self.userbox)\
            .click\
            .send_keys(username)\
            .move_to_element(self.pw_box)\
            .click\
            .send_keys(password)\
            .move_to_element(self.login_button)\
            .click()
        
        # return instance of the following page
        return InstaFeedPage(self.browser) 

InstaPy

automating Instagram

Open Source

1.8k stars
26 contributor

375 forks

InstaPy

Don't like keywords

Ignore users

Ignoring restrictions

Restrictions

Clarifai - Smart image filtering

Emoji Support

Limit by followers

Works on a Server

Docker support

Clarifai -comments based on image tags

Works without API

Detailed documentation

InstaPy

from instapy import InstaPy

InstaPy(username='EuroPython', password='Rocks!')\
  .login()\
  .set_upper_follower_count(limit = 2500) \
  .set_do_comment(True, percentage=10) \
  .set_do_follow(True, percentage=10) \
  .set_comments(['Cool!', 'Awesome!', 'Nice!']) \
  .set_dont_include(['myFriend', 'myOtherFriend']) \
  .set_dont_like('NSFW', 'naked') \
  .like_by_tags(['nature', 'cat'], amount=100) \
  .end()

InstaPy Growth

A quick look at my Instagram growth.

InstaPy Growth

A quick look at my Instagram growth.

InstaPy Growth

A quick look at my Instagram growth.

Open Source

Why You should be doing it!

Open Source

Why You should be doing it!

- Give something back

Open Source

Why You should be doing it!

- Give something back

- Build a portfolio

Open Source

Why You should be doing it!

- Give something back

- Build a portfolio

- Get opportunities

Open Source

What it did for Me!

Open Source

What it did for Me!

7 job offers

 

Open Source

What it did for Me!

7 job offers

9 internship offers

 

Open Source

What it did for Me!

7 job offers

9 internship offers

A ton of new contacts

Open Source

What it did for Me!

7 job offers

9 internship offers

A ton of new contacts

This talk ;)

Open Source

How to level up Your open source portfolio!

Open Source

How to level up Your open source portfolio!

- Add documentation 

 

Open Source

How to level up Your open source portfolio!

- Add documentation 

- Spread the word

 

Open Source

How to level up Your open source portfolio!

- Add documentation 

- Spread the word

- Support the users

 

What's next?

Help Us bring InstaPy to the next level!

@timigrossmann

/timgrossmann

/@TimGrossmann

contact.timgrossmann

Automating Instagram

By Tim Großmann

Automating Instagram

  • 1,690