본문 바로가기
파이썬/파이썬

[ 파이썬 ] 캠핑장 예약 조회(실시간)-데이터 테이블 비교

by jeong-f 2022. 1. 14.
반응형

 

일정 시간 간격으로 데이터를 조회하여 데이터 테이블로 만든 후 갱신한 데이터와 비교하여 다른 부분이 발생한 경우 출력하기 위해 만들었습니다.

 

이전 포스팅

 

[ 파이썬 ] 캠핑장 예약 조회

파이썬에서 URL 정보를 받아 예약 가능한 사이트만 조회하는 정보를 만들고자 합니다. 아래 순서로 데이터를 가져옵니다. 사이트 접속 데이터 프레임 변환 예약 가능한 사이트만 가져오기(데이

jeong-f.tistory.com

소스 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'''
pip install urllib3
pip install pandas 
 
'''
 
import requests
import urllib3
import pandas as pd
import schedule
import time
import datetime
import os
 
#--------------------------------------------------------------------
def get_datataframe( initYear,initMonth,initDay,delNotBooking=True):
    
    urllib3.disable_warnings() # HTTPS 연결시 TLS 1.0 사용
    url = 'https://www.sj.go.kr/gumsu/page.do?mnu_uid=1850&csr_class=0&step=list&cs_uid=0'
    url += "&initYear=" +initYear
    url += "&initMonth=" +initMonth
    url += "&initDay=" +initDay
    print(url)
 
    # 웹페이지를 읽어 데이터 프레임으로 변환
    dfs = pd.read_html(url,encoding='utf-8')
 
    # 두번째 프레임(0부터시작)이 예약정보    
    df2= dfs[1]
    #print(df2)
 
    #예약하기가 아니면 삭제
    if delNotBooking:
        condition = (df2.예약하기 == '예약하기')
        df2 = df2.loc[condition]
 
    # 불필요한 세번째 열(0 부터 시작) '규모(m2)' 삭제
    df2 = df2.drop(df2.columns[2],axis=1)
    #print(df2)
 
    return df2
#--------------------------------------------------------------------
def schedule_t1():
    
    global old_links
    
    now = datetime.datetime.now()
    nowDatetime = now.strftime('%Y-%m-%d %H:%M:%S')
    print(nowDatetime)
 
    df_old = pd.DataFrame(old_links)
    df_new = get_datataframe('2021','6','03',False)
    #print(df_new)
        
    if "예약하기" in df_old: 
        merge = pd.merge(old_links,df_new,indicator=True,on='예약하기', how='outer' )
        differnece = merge[merge['_merge']=='right_only']
        if len(differnece) > 0 :
            print(differnece)
            
    old_links = pd.DataFrame(df_new)
 
#--------------------------------------------------------------------
 
if __name__ == '__main__':
  
    old_links = {}
        
    schedule.every(10).seconds.do(schedule_t1) #10초마다 동작
    #os.system("pause")
    
    #무한 루프를 돌면서 스케쥴을 유지한다.
    while True:
        schedule.run_pending()
        time.sleep(1)
cs
반응형

댓글