Download profile pictures from Instagram using python

In some cases, we all would have wanted to download the profile picture of a person from Instagram. On Instagram we can only be able to see the profile picture of a user as a very small thumbnail. What if we could view the profile picture of the user and even save it? Python let us do that just with the username of the profile and of-course with some lines of code.

Let us get to the coding part.

The code

This will download the profile picture of Selena Gomez in the current directory.

Selena Gomez image downloaded by the script

How does it work?

  1. Get the username as the input from the user.
  2. Pass the input to the image function.
  3. Create a session object from the requests library.
  4. Get the HTML code using the Beautiful soup library.
  5. Using regular expression get find “profile_pic_url_hd”
  6. Get the contents of the URL and download it.

Using library

Instead of this code, you can also use the “ig-profile” library which will just have this code and you can directly call the image function from the library.

Pip installation

Install the library using the pip.

pip install ig-profile

Example

from ig_profile import ProfileImage as pimage
pimage.image(YOUR_USER_NAME)

The above code will simply download the desired profile picture and open it up in a new window.