[토이 프로젝트] KTX/SRT 잔여석 알림

2019. 9. 9. 03:01Projects

개요

프로젝트 명 : KTX / SRT 잔여석 알림

프로젝트 구분 : 토이 프로젝트

프로젝트 기간 : 2017년 01월

참가 인원 : 1명

설명

파이썬을 이용한 데이터 크롤링, 분석 연습을 위하여 설 기차 잔여석 알림 크롤러를 개발하였습니다.
구동은 가상서버에서 crontab을 이용하여 1분마다 지정 된 스크립트를 실행시켜 KTX와 SRT의 잔여석 여부를 확인하고 해당 시간을 메일을 통해 전송합니다.

소스코드 (Mail)

#-*- encoding: utf-8 -*-#

import smtplib  

# 메일 발송
def send_mail(from_user, to_user, subject, text):  
	gmail_user = 'mail@gmail.com'
	gmail_pwd = 'password'
	smtpserver = smtplib.SMTP("smtp.gmail.com",587)
	smtpserver.ehlo()
	smtpserver.starttls()
	smtpserver.ehlo
	smtpserver.login(gmail_user, gmail_pwd)
	header = 'To:' + to_user + '\n' + 'From: ' + from_user + '\n' + 'Subject:'+subject+' \n'
	msg = header + '\n '+text+' \n\n'
	smtpserver.sendmail(from_user, to_user, msg)
	print 'mail send'
	smtpserver.close()

소스코드 (SRT)

#!/usr/bin/python
#-*- encoding: utf-8 -*-#

import requests
from bs4 import BeautifulSoup
# 본 코드를 사용하기 위해서는 send_mail 함수를 가진 mail 모듈을 직접 작성하여야합니다.
from mail import send_mail

for i in range(0, 3):
	time = 12 + (i * 4)
	dptTm = str(time)+'0000'

	# SRT 데이터 크롤링
	url = 'https://etk.srail.co.kr/hpg/hra/01/selectScheduleList.do?pageId=TK0101010000'
	postData = {'dptRsStnCd':'0551'
	,'arvRsStnCd':'0015'
	,'stlbTrnClsfCd':'05'
	,'trnGpCd':'109'
	,'psgNum':'1'
	,'seatAttCd':'015'
	,'isRequest':'Y'
	,'dptRsStnCdNm':'수서'
	,'arvRsStnCdNm':'동대구'
	,'dptDt':'20170126'
	,'dptTm':dptTm
	,'chtnDvCd':'1'
	,'psgInfoPerPrnb1':'1'
	,'psgInfoPerPrnb5':'0'
	,'psgInfoPerPrnb4':'0'
	,'psgInfoPerPrnb2':'0'
	,'psgInfoPerPrnb3':'0'}

	data = requests.post(url, postData)
	plain_text = data.text
	soup = BeautifulSoup(plain_text, 'lxml')

	result = soup.find('tbody')

	count = 0
	content = ''
	content += str('17년 1월 26일 수서 - 동대구 SRT 예매가 가능합니다.\n')

	while result.find_next('tr') is not None:
		result = result.find_next('tr')
		# 예약 가능 검사
		if result.find('a', {"class":"button-02"}) is not None:
			# 예약 가능
			count += 1
			temp = result.find('td', {"class":"trnNo"})
			start = temp.find_next('td')
			end = start.find_next('td')

			start = start.find('em')
			end = end.find('em')
			content += str(start.text+' ~ '+end.text+'\n')

	if count > 0:
		send_mail("from@mail.com", "to@mail.com", "title", str(content))

소스코드 (KTX)

#!/usr/bin/python
#-*- encoding: utf-8 -*-#

import requests
from bs4 import BeautifulSoup
import mail

# 코레일 데이터 크롤링
url = 'http://www.letskorail.com/ebizprd/EbizPrdTicketPr21111_i1.do'
postData = {'selGoTrain':'05'
,'txtPsgFlg_1':'1'
,'txtPsgFlg_2':'0'
,'txtPsgFlg_3':'0'
,'txtPsgFlg_4':'0'
,'txtPsgFlg_5':'0'
,'txtSeatAttCd_3':'000'
,'txtSeatAttCd_2':'000'
,'txtSeatAttCd_4':'015'
,'selGoTrainRa':'05'
,'radJobId':'2'
,'srtCheckYn':'Y'
,'txtGoStart':'서울'
,'txtGoEnd':'영천'
,'selGoYear':'2017'
,'selGoMonth':'01'
,'selGoDay':'26'
,'selGoHour':'19'
,'txtGoHour':'190000'
,'txtGoYoil':'목'
,'selGoSeat1':'015'
,'txtPsgCnt1':'1'
,'txtPsgCnt2':'0'
,'txtGoPage':'1'
,'txtGoAbrdDt':'20170126'
,'checkStnNm':'Y'
,'SeandYo':'N'
,'chkInitFlg':'Y'
,'txtMenuId':'11'
,'ra':'1'
,'strChkCpn':'N'
,'txtSrcarCnt':'0'
,'txtSrcarCnt1':'0'
,'hidRsvTpCd':'03'
,'txtPsgTpCd1':'1'
,'txtPsgTpCd2':'3'
,'txtPsgTpCd3':'1'
,'txtPsgTpCd5':'1'
,'txtPsgTpCd7':'1'
,'txtDiscKndCd1':'000'
,'txtDiscKndCd2':'000'
,'txtDiscKndCd3':'111'
,'txtDiscKndCd5':'131'
,'txtDiscKndCd7':'112'
,'txtCompaCnt1':'0'
,'txtCompaCnt2':'0'
,'txtCompaCnt3':'0'
,'txtCompaCnt4':'0'
,'txtCompaCnt5':'0'
,'txtCompaCnt6':'0'
,'txtCompaCnt7':'0'}

data = requests.post(url, postData)
plain_text = data.text
soup = BeautifulSoup(plain_text, 'lxml')

result = soup.find('table', id="tableResult")
result = result.find('td', title="KTX")

# 좌석 매진 검사
count = 0

for i in range(0, 6):
	result = result.find_next('td')
	data = str(result)

	if data.find('btn_selloff.gif') > 0:
		count += 1

if count < 3:
	send_mail("qortlduf505@gmail.com", "qortlduf505@gmail.com", "[크롤러 알림] 1월 27일 기차 예매 가능", "17년 1월 27일 서울 - 동대구 KTX 예매가 가능합니다.")