In this post, you’ll learn how to build Instagram scrapers to automate your information retrieval and build a nice GUI using Best Web Scraping Tools python. You can build scalable Instagram scrapers and many automation functionalities easily by combining Instaloader and Python4Delphi library, inside Delphi and C++Builder.
Table of Contents
What is Instaloader?
Instaloader is a tool to download Instagram pictures (or videos) and retrieve their captions and other metadata.
The following are Instaloader features and functionalities:
- Downloads public and private profiles, hashtags, user stories, feeds, and saved media.
- Downloads comments, geotags, and captions of each post.
- Automatically detects profile name changes and renames the target directory accordingly.
- Allows fine-grained customization of filters and where to store downloaded media.
- Automatically resumes previously interrupted download iterations.
This post will guide you on how to run the Instaloader library for scraping the data from Instagram, displaying it in the Delphi Windows GUI app, and downloading all the data to your computer.
How do I install the Instaloader library?
First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.
Here are some examples of how Instaloader would easily solve your Social Media data collection problems:
How can we extract valuable information from an Instagram profile?
Let’s extract some valuable information about the @embarcaderotech Instagram account automatically, using this script inside Python4Delphi GUI:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Import the module import instaloader # Create an instance of Instaloader class bot = instaloader.Instaloader() # Load a profile from an Instagram handle profile = instaloader.Profile.from_username(bot.context, 'embarcaderotech') print(type(profile)) print("Username: ", profile.username) print("User ID: ", profile.userid) print("Number of Posts: ", profile.mediacount) print("Followers: ", profile.followers) print("Followees: ", profile.followees) print("Bio: ", profile.biography,profile.external_url) |
The script above will print Username, User ID, Number of Posts, Followers, Followings, Bio, and External URL seamlessly:
How can We Download All Images and Videos from Instagram Users, Along with their Captions?
Python’s Instaloader allows us to get any posts from any public profile easily. We just need to use the get_posts() method. We will use this method on the profile of @embarcaderotech.
Let’s download each post image/video and caption, by looping over the generator object using the .download_post() method. Run the following script in Python4Delphi GUI:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Import the module import instaloader # Create an instance of Instaloader class bot = instaloader.Instaloader() # Load a profile from an Instagram handle #profile = instaloader.Profile.from_username(bot.context, 'embarcaderotech') # Get all posts in a generator object posts = profile.get_posts() # Iterate and download for index, post in enumerate(posts, 1): bot.download_post(post, target=f"{profile.username}_{index}") |
It will save the post and create new folders with folder name “embarcadero_1” until “embarcadero_n”, inside the directory that contains Python4Delphi Demo01.exe that we use to run all the scripts above.
In each folder, you will see the actual content of the posts of the profile like a video or images. The scripts above are taken and modified from this post.
Here is the output in Python4Delphi GUI:
All the contents will be retrieved automatically to the directory where you save or run the Python4Delphi Demo01 GUI:
You will get all the contents you need in no time (compared with the hard work to download them manually)!
What do the Instagram items look like when they are downloaded?
This is what the contents of each folder look like:
Works like magic, isn’t it?
Congratulations, now you have learned how to run the Instaloader library for scraping the data from Instagram, displaying it in the Delphi Windows GUI app, and downloading all the data to your computer! Now you can scrape any data from any public accounts you are interested in using the Instaloader library and Python4Delphi.
Check out the Instaloader library for Python and use it in your projects: https://pypi.org/project/instaloader/ and
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi