利用Python爬取PM2.5數據

程式來源於https://github.com/ShaoQiBNU/python-PM2.5
僅針對csv的編碼(UTF8)及地點(高雄)進行修改

每小時爬取一次

import time
from threading import Timer
import pandas as pd
import numpy as np
def delayrun():
while True:
file_time=time.strftime('%Y-%m-%d-%H',time.localtime(time.time()))
file_name='C:/Users/user01/Desktop/'+file_time+'-PM.csv'
data=pd.read_html('http://www.86pm25.com/city/gaoxiong.html')[0]
data=data.ix[:,[0,1,3,4]].copy()
data.ix[:,2]=data.ix[:,2].str.replace('μg/m³','')
data.ix[:,3]=data.ix[:,3].str.replace('μg/m³','')
data.to_csv(file_name,index=False, encoding='utf_8_sig')
timer_interval=3600
t=Timer(timer_interval,delayrun)
t.start()

爬取完後休眠

import time
import pandas as pd
import numpy as np
while True: file_time=time.strftime('%Y-%m-%d-%H',time.localtime(time.time()))
file_name='C:/Users/user01/Desktop/'+file_time+'-PM.csv'
data=pd.read_html('http://www.86pm25.com/city/gaoxiong.html')[0]
data=data.ix[:,[0,1,3,4]].copy()
data.ix[:,2]=data.ix[:,2].str.replace('μg/m³','')
data.ix[:,3]=data.ix[:,3].str.replace('μg/m³','')
data.to_csv(file_name,index=False, encoding='utf_8_sig')
time.sleep(3600)

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store