728x90
반응형

 

도메인에 알맞는 자신만의 데이터 세트를 만들기 위해서는 

공개된 데이터 세트를 이용할 수 도 있지만, 추가적으로 인스타그램에 떠돌아다니는 이미지를 수집하여 저장할 수 있다. 

 

인스타그램 크롤러를 통해서 이미지를 수집하는 것은 인스타그램 자체에서 속도를 제한시켜놓았기 때문에

1000장 이상 모으고자 할 경우, 상당히 느리게 수집된다는 단점이 있다. 

또한 한 해쉬태그 당 최대 10000장 까지만 수집이 가능하다.

 

https://github.com/huaying/instagram-crawler

 

huaying/instagram-crawler

Get Instagram posts/profile/hashtag data without using Instagram API - huaying/instagram-crawler

github.com

 

 

방법은 굉장히 간단하다. 

 

1. (크롬 설치 및) 크롬 버전 확인하기 

 

... 표시로 되어있는 설정 > Help > About Google Chrome 

 

 

 

2. 크롬 드라이버 다운로드 

 

https://sites.google.com/a/chromium.org/chromedriver/downloads

 

Downloads - ChromeDriver - WebDriver for Chrome

WebDriver for Chrome

sites.google.com

필자는 73.0.3683 ... 버전을 사용중이여서 ChromeDriver 73.0.3683.68 을 다운받았다.

 

 

2-1. 크롬 드라이버

 

다운 받은 크롬 드라이버를 ./inscrawler/bin/chromedriver 에 위치 시켜준다.

 

 

 

 

3. Selenium 설치

~$ pip install -r requirements.txt

 

이 때 black==19.3b0 버전 에러가 나는 분들은 python 3.6 을 이용하여 설치해야한다.

(black 은 코어가 python 3.6 기반이라고 한다.) 

~$ python3 -m pip install -r requirements.txt 

 

 

4. 필요한 파일 복사하기

~$ cp inscrawler/secret.py.dist inscrawler/secret.py

 

 

5. 크롤러를 통해 URL 저장하기 

 

자세한 사용법은 https://github.com/huaying/instagram-crawler 을 참고

 

$ python crawler.py hashtag -t cake -o ./output.json

위와 같은 명령어는 "cake" 라는 해시태그를 통해 output.json 이라는 파일로 이미지의 url 을 저장하겠다는 명령어이다. 

 

 

$ python crawler.py hashtag -t cake -o ./output.json -n 10000

위는 10000개의 이미지의 url을 저장하겠다는 명령어이다. 

 

 

 

시간이 어느 정도 지나 json 파일이 만들어지면, 아래와 같이 나타나고

 

 

6. URL을 이용하여 이미지 다운로드 

 

이 이미지의 url 정보가 포함된 json 파일을 이용하여 영상을 다운로드 할 수 있다. 

 

Python 코드는 다음과 같다. 

import urllib.request
import json


def DownloadSingleFile(fileURL, cnt):
	print('Downloading image...')

	fileName = '/instagram-crawler/insta' + str("%06d"%cnt) + '.jpg'
	urllib.request.urlretrieve(fileURL, fileName)
	print('Done. ' + fileName)


if __name__ == '__main__':
	
	with open('/instagram-crawler/output.json') as data_file:    
		data = json.load(data_file)

	for i in range(0, len(data)):
		instagramURL = data[i]['img_url']
		DownloadSingleFile(instagramURL, i)
~$ python download.py

 

insta000000.jpg

insta000001.jpg 

insta000002.jpg

 

...

 

이러한 형태로 저장된다.  

728x90
반응형