วันอาทิตย์ที่ 13 ธันวาคม พ.ศ. 2563

Python Web Scraping Covid-19 Line Notify | เขียน Python Web Scraping แบบบ้านๆ ส่งเข้า Line Notify

 ตัวอย่างแรกจะเป็นการเข้าไปตรวจสอบยอดจากกรมควมคุมโรค

ที่มา : Covid-19 Infected Situation Reports (Thailand) (moph.go.th)



---- Start Code ---

import requests

import json

from songline import Sendline


#line token

token = 'xxxxxx'


url = 'https://covid19.th-stat.com/api/open/today'

header = {'X-Requested-With':'XMLHttpRequest'}

response = requests.get(url,headers=header)

x = json.loads(response.text)


all_case = x['Confirmed']

resolved_case = x['Recovered']

new_case = x['NewConfirmed']

death_case = x['Deaths']

update = x['UpdateDate']

newrecov_case = x['NewRecovered']

new_death = x['NewDeaths']


check_update = open("check_update.txt","r")

xx = check_update.read()

if xx == update:

    print("no update")

else:

    a = ('รายงาน ยอด Covid-19 Thai \nUpdate : {}น.\nทั้งหมด : {} ราย, รายไหม่ :{} ราย,\n\

หายแล้ว : {} ราย, หายเพิ่ม : {} ราย,\nเสียชีวิตทั้งหมด : {} ราย, เสียชีวิตเพิ่ม : {} ราย'

         .format(update,all_case, new_case,resolved_case,newrecov_case,death_case,new_death ))


    messenger = Sendline(token)


    f = open("check_update.txt","w+")

    f.write(update)

    f.close() 


    messenger.sendtext(a)

--------------- End Code ---------------------

+ ก่อนรันจะต้องสร้างไฟล์ check_update.txt ก่อนน่ะครับ เพื่อเก็บวันที่สำหรับตรวจว่าเป็นข้อมูลเก่าหรือข้อมูลใหม่

+ สำหรับ line token ลองหาใน google มีสอนเยอะมาก



สำหรับตัวอย่างที่ 2 เป็นการดึงรายงานจาก ศูนย์สื่อสาร COVID-19 จังหวัดปัตตานี

url : COVID-19 PATTANI


----------- Start Code -----------------

import requests

import json

from songline import Sendline

import datetime


#Line Token

token = 'xxxxx'


#url_date = 'http://203.157.237.20/covid_report/dist/index.php'

url = 'http://203.157.237.20/covid_report/dist/ajax/read_p_covid_detail.php'

url_PatientNumber = 'http://203.157.237.20/covid_report/dist/ajax/read_p_n.php'


header = {'X-Requested-With':'XMLHttpRequest'}

response = requests.get(url,headers=header)

response_PatientNumber = requests.get(url_PatientNumber,headers=header)

result = json.loads(response.text)

result_PatientNumber = json.loads(response_PatientNumber.text)


pui_total = result['pui_total']  #ผู้เข้าเกณฑ์เฝ้าระวัง (PUI) สะสม

no_covid_total = result['no_covid_total'] #สำรวจไม่พบเชื้อ สะสม

in_hos_province = result['in_hos_province']#อยุ่ รพ.ในจังหวัด

out_hos_province = result['out_hos_province'] #ส่งตัวกลับจาก รพ.ต่างจังหวัด

in_hos_fild = result['in_hos_fild']#อยู่ รพ.สนาม

more_d = result['more_d']# State Quarantine (SQ) รายใหม่

mode_h = result['mode_h']# State Quarantine (SQ) สะสม  

backhome = result['backhome']#กลับบ้าน

dead = result['dead']#เสียชีวิต

dead_amphur = result['dead_amphur']#

wait_result_lab = result['wait_result_lab']#รอผลตรวจ 

patient_new = result['patient_new']# ผู้ป่วยพบเชื้อสะสมเพิ่ม

refer_m = result['refer_m']#Refer ไป รพ.มอ


#ผู้ป่วยพบเชื้อสะสม

patients = result_PatientNumber['patients']


#check last update not found info from url source

datecheck = datetime.datetime.now()

update = (datecheck.strftime("%c"))


# show full msg

info = 'ศูนย์สื่อสาร COVID-19 จังหวัดปัตตานี'


fullmsg = ('รายงาน ยอด Covid-19 Pattani \nUpdate check : {}\nผู้เข้าเกณฑ์เฝ้าระวัง (PUI) สะสม : {} ราย,\n\

สำรวจไม่พบเชื้อ สะสม : {} ราย,\nอยุ่ รพ.ในจังหวัด : {} ราย,\nส่งตัวกลับจาก รพ.ต่างจังหวัด : {} ราย,\nอยู่ รพ.สนาม : {} ราย, กลับบ้าน : {} ราย,\n\

State Quarantine (SQ) สะสม/รายใหม่: {}/{} ราย,\n\

เสียชีวิต : {} ราย,\nรอผลตรวจ : {} ราย,\nผู้ป่วยพบเชื้อสะสม/เพิ่ม : {}/{} ราย\n\

ที่มา : {}'

.format(update,pui_total, no_covid_total,in_hos_province,out_hos_province,in_hos_fild,backhome,mode_h,more_d,dead,

        wait_result_lab,patients,patient_new,info ))


#print(fullmsg)

messenger = Sendline(token)

messenger.sendtext(fullmsg)

------------  End Code ----------------------

สำหรับตัวอย่างที่2 นี้ ไฟล์ข้อมูลอยู่2 ที่ เท่าที่เข้าใจน่ะครับ Code เลยใช้แบบง่ายๆ  

เนื่องจากว่าพยายามหาวันที่อัพเดทของรายงาน หาไม่เจอเลยตั้งให้แสดงวันเวลาที่รันไฟล์ครับ

การใช้งานก็สามารถประยุกต์ไปสร้าง schedule run file ตามแล้วแต่เลยครับ 

เรื่องการ install library ของ python ลองหาใน google ดูน่ะครับ