네이버에서 제공하는 주가정보를 가져와 SQLite에 저장합니다.

Python의 asyncio를 활용하여 단일 스레드에서 시분할로 작동하게끔 만들었는데, 아래의 작업들을 개별 coroutine으로 분리하여 network IO, disk IO에 따른 block이 발생하면 다른 coroutine으로 제어권이 넘어갑니다.

  • 네이버 웹서버에 접속하여 데이터 가져오기
  • 가져온 정보 파싱하기
  • 데이터베이스에 저장하기

실행방법 (8월부터 삼성전자(005930) 주가와 거래량 가져오기) python3 fetch.py 2016-08-01 2016-09-04 005930 015760

실행결과

[INFO|fetcher.py:16] 2016-09-04 21:28:07,950 > Starting process (pid:1336)
[INFO|fetcher.py:17] 2016-09-04 21:28:07,950 > This pricess will fetch interday prices from NAVER
[INFO|fetcher.py:29] 2016-09-04 21:28:07,950 > Connecting to database, interday.db
[DEBUG|fetcher.py:61] 2016-09-04 21:28:07,950 > Preparing initial 1 tasks for event loop
[DEBUG|fetcher.py:64] 2016-09-04 21:28:07,950 > Entering an event loop
[DEBUG|fetcher.py:84] 2016-09-04 21:28:07,950 > Fetching task for symbol 015760
[DEBUG|fetcher.py:99] 2016-09-04 21:28:07,950 > Requesting http://finance.naver.com/item/sise_day.nhn?code=015760&page=1
[DEBUG|fetcher.py:116] 2016-09-04 21:28:08,043 > Parsing 10,204 bytes from symbol 015760 (page: 1)
[DEBUG|fetcher.py:159] 2016-09-04 21:28:08,075 > Saving 2 days of symbol 015760
[DEBUG|fetcher.py:76] 2016-09-04 21:28:09,075 > Closing the event loop
[DEBUG|fetcher.py:79] 2016-09-04 21:28:09,075 > 2 rows were inserted into database
[DEBUG|fetcher.py:80] 2016-09-04 21:28:09,075 > Closing the database connection
[Finished in 1.5s]

소스코드