프로그래밍/Python

Python - APScheduler

1q 2016. 12. 27. 21:44
스크래핑을 진행할 때 APScheduler를 사용해 시간을 정하여 스크랩을 진행하게 할 수 있습니다.

APScheduler의 BlockingScheduler() 를 사용할 것이며 사용 방법은 기존 스크래핑 코드를 함수로 묶어준 후 APScheduler의 인터벌을 사용하여 정한 시간마다 한 번씩 코드가 실행되도록 할 수 있습니다.

1. 설치

1
pip install APScheduler
cs


2. 정리

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import os
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
 
def scraping():
    print datetime.strftime(datetime.now(), "suspect_%Y-%m-%d_%H:%M:%S")
 
if __name__ == '__main__':
    scheduler = BlockingScheduler()
    
    # job을 등록
    scheduler.add_job(
        scraping,
        'interval',
        seconds=5# 60초마다 실행
    try:
        scheduler.start()
    except(KeyboardInterrupt, SystemExit):
        print "End"
        pass
 
 
cs


3. 결과

- 5초에 한 번씩 찍히는 것을 볼 수가 있다.