IT

Python: Instaloader(인스타로더) 패키지 소개

유병혁 2022. 5. 12. 02:23

안녕하세요? 이번 글은 Instagram 이미지를 다운로드할 수 있는 'Instaloader(인스타로더)' 패키지 사용법을 간략히 실습해 보겠습니다. Beautiful Soup나 Selenium으로 직접 크롤링(crawling: 웹 페이지를 그대로 가져와서 거기서 데이터를 추출해 내는 행위)하지 않고도 Python 패키지로 손쉽게 이미지를 내려받을 수 있는 방법입니다.

 

Instaloader — Download Instagram Photos and Metadata

It is a pleasure for us to share our Instaloader to the world, and we are proud to have attracted such an active and motivating community, with so many users who share their suggestions and ideas with us. Buying a community-sponsored beer or coffee from ti

instaloader.github.io

Python이 설치된 상태에서 아래 명령어로 Instaloader를 설치합니다.

pip install instaloader

필요한 모듈을 호출합니다.

import instaloader
from instaloader import Profile, Post

인스턴스(instance)를 생성한 후, 내 Instagram 계정(your_username, your_password 내용 교체)으로 로그인합니다. *Instagram 웹페이지에서 해당 계정으로 로그인해보신 후 실습을 진행합니다.

instance = instaloader.Instaloader()
instance.login(user="your_username",passwd="your_password")

실습을 위해 인스타그램에서 '#국립공원 해시태그'를 검색해 보겠습니다.

 

#국립공원 해시태그 • Instagram 사진 및 동영상

 

www.instagram.com

목록 중 단일 게시물 1개를 내려받아보겠습니다. 이미지를 클릭한 후, URL 주소 중 'https://www.instagram.com/p/' 이후 위치한 ID 값을 복사하여 아래 코드에 반영해 주시면 됩니다. 다운로드가 완료되었으면 'True'라는 값이 반환될 것입니다.

post = Post.from_shortcode(instance.context, "ID 값")
instance.download_post(post,target="새 폴더")

새로 생성된 폴더에는 다음과 같이 해당 이미지(*.jpg), 캡션(*.txt), 댓글 스레드(*.json)가 위치하고 있습니다.

이번에는 '#국립공원 해시태그' 이미지 10개 게시물을 다운로드 받아보겠습니다. 아래와 같이 설정하시면 됩니다. 다운로드할 최대 게시물 수를 지정하지 않으면, 지정된 해시태그 아래에 있는 모든 단일 게시물을 자동으로 다운로드하려고 시도할 것입니다. 결과는 아래와 같습니다.

instance.download_hashtag(hashtag="국립공원",max_count=10)

Instaloader 사용법은 Yaniss Illoul(야니스 일룰) 님의 블로그 글이 유익한 것 같습니다. 참고하시면 좋겠습니다.

 

How to Download an Entire Account Instagram Posts and Stories with Python

Join the top marketers who read our newsletter each week.

martechwithme.com