금 시세 기록을 위해 만든 python 스크립트
1. BeautifulSoup을 설치하자.
$ sudo apt-get install python-bs4
2. 네이버 금시세를 얻어와서 출력하자. Text file로 만들거니까..
# gold.py
import requests
from bs4 import BeautifulSoup
page = 1
while (1):
reqstr = 'http://info.finance.naver.com/marketindex/goldDailyQuote.nhn?page=' + str(page)
req = requests.get(reqstr)
html = req.text
soup = BeautifulSoup(html, 'html.parser')
my_date = soup.select(
'body > div > table > tbody > tr > td:nth-of-type(1)'
)
my_values = soup.select(
'body > div > table > tbody > tr > td:nth-of-type(2)'
)
if (len(my_values) <= 0):
break
row = 0
for title in my_values:
print(my_date[row].text + '\t' + title.text)
row = row + 1
page = page + 1
3. 매일 실행시키려면, crontab 등록을 위해 shell script를 하나 만들자
#/bin/sh python /home/dasomoli/src/gold.py > /mnt/NAS/Data/Gold/`date +%F`.txt
4. crontab에 등록하자
$ crontab -e
0 1 * * * /home/dasomoli/src/record_gold.sh
DB만들어서 기록하면, 한번 요청으로 끝나겠지만 귀찮고, 육아로 시간이 없으므로 오늘은 여기까지!