Welcome to Instapy’s documentation!

Automating the Browser

Oddly is proving to be quite difficult.

The methods of a selenium.Firefox() object

pprint.pprint(dir(browser))
['CONTEXT_CHROME',
'CONTEXT_CONTENT',
'NATIVE_EVENTS_ALLOWED',
'__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__enter__',
'__eq__',
'__exit__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'_file_detector',
'_is_remote',
'_mobile',
'_switch_to',
'_unwrap_value',
'_web_element_cls',
'_wrap_value',
'add_cookie',
'application_cache',
'back',
'binary',
'capabilities',
'close',
'command_executor',
'context',
'create_web_element',
'current_url',
'current_window_handle',
'delete_all_cookies',
'delete_cookie',
'desired_capabilities',
'error_handler',
'execute',
'execute_async_script',
'execute_script',
'file_detector',
'file_detector_context',
'find_element',
'find_element_by_class_name',
'find_element_by_css_selector',
'find_element_by_id',
'find_element_by_link_text',
'find_element_by_name',
'find_element_by_partial_link_text',
'find_element_by_tag_name',
'find_element_by_xpath',
'find_elements',
'find_elements_by_class_name',
'find_elements_by_css_selector',
'find_elements_by_id',
'find_elements_by_link_text',
'find_elements_by_name',
'find_elements_by_partial_link_text',
'find_elements_by_tag_name',
'find_elements_by_xpath',
'firefox_profile',
'forward',
'fullscreen_window',
'get',
'get_cookie',
'get_cookies',
'get_log',
'get_screenshot_as_base64',
'get_screenshot_as_file',
'get_screenshot_as_png',
'get_window_position',
'get_window_rect',
'get_window_size',
'implicitly_wait',
'install_addon',
'log_types',
'minimize_window',
'mobile',
'name',
'orientation',
'page_source',
'profile',
'quit',
'refresh',
'save_screenshot',
'service',
'session_id',
'set_context',
'set_page_load_timeout',
'set_script_timeout',
'set_window_position',
'set_window_rect',
'set_window_size',
'start_client',
'start_session',
'stop_client',
'switch_to',
'switch_to_active_element',
'switch_to_alert',
'switch_to_default_content',
'switch_to_frame',
'switch_to_window',
'title',
'uninstall_addon',
'w3c',
'window_handles']

So by count that’s like 130 methods. vars(browser) didn’t give me a ton so apparently the magic is in the methods.

API

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python
"""Script that functionally uses the basic API for InstaPy."""

from instapy_00 import username, password
from instapy import InstaPy


def start_session():
    session = InstaPy(username="Chugthai", password="pyQV7GcQVjBsU93mFy48")

    session.login()

    session.like_by_tags(["bmw", "mercedes"], amount=5)
    session.set_dont_like(["naked", "nsfw"])

    # Next, you can tell the bot to not only like the posts but also to follow some
    # of the authors of those posts. You can do that with set_do_follow():
    session.set_do_follow(True, percentage=50)

    # You can also leave some comments on the posts. There are two things that you
    # need to do. First, enable commenting with set_do_comment():
    session.set_do_comment(True, percentage=50)

    # now let's set what comments to leave with this
    # session.set_comments()  # type: List
    session.set_comments(["Nice!", "Sweet!", "Beautiful :heart_eyes:"])

    session.end()


if __name__ == "__main__":
    start_session()

Additional Features in InstaPy

InstaPy is a sizable project that has a lot of thoroughly documented features. The good news is that if you’re feeling comfortable with the features you used above, then the rest should feel pretty similar. This section will outline some of the more useful features of InstaPy.

Headless Browser

This feature allows you to run your bot without the GUI of the browser. This is super useful if you want to deploy your bot to a server where you may not have or need the graphical interface. It’s also less CPU intensive, so it improves performance. You can use it like so:

Quota Supervisor Intro

You can’t scrape Instagram all day, every day. The service will quickly notice that you’re running a bot and will ban some of its actions. That’s why it’s a good idea to set quotas on some of your bot’s actions.

Using AI to Analyze Posts

Earlier you saw how to ignore posts that contain inappropriate words in their descriptions. What if the description is good but the image itself is inappropriate? You can integrate your InstaPy bot with ClarifAI, which offers image and video recognition services: Now your bot won’t like or comment on any image that ClarifAI considers NSFW. You get 5,000 free API-calls per month.

Relationship Bounds

It’s often a waste of time to interact with posts by people who have a lot of followers. In such cases, it’s a good idea to set some relationship bounds so that your bot doesn’t waste your precious computing resources: With this, your bot won’t interact with posts by users who have more than 8,500 followers.

Contents:

Indices and tables