1

## PYTHON 想寫個天氣的爬蟲 請問資料要去哪邊撈 台灣 世界的都可以給

PYTHON 想寫個天氣的爬蟲 請問資料要去哪邊撈

### 1 個回答

13
japhenchen
iT邦大師 1 級 ‧ 2021-03-30 15:47:26

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import folium
import jsonpickle
import requests
import re
import os
import datetime
import colorsys
import math

ismain = False
testName = 'AQI'

#取得空品最大值與最小值轉換成相對色彩值
def colorWVB(From=[0,0,0],To=[360,100,100],r=0,min=0,max=30):
#colorMin = colorsys.hsv_to_rgb(360,0,100)
#colorMax = colorsys.hsv_to_rgb(272,100,43)
sColor='white'
cH = [From[0],To[0]]
cS = [From[1],To[1]]
cV = [From[2],To[2]]
if max>min:
vR = (r-min)/(max-min)
sH = ((cH[-1]-cH[0])*vR+cH[0])/360
sS = ((cS[-1]-cS[0])*vR+cS[0])/100
sV = ((cV[-1]-cV[0])*vR+cV[0])/100
rgb = colorsys.hsv_to_rgb(sH,sS,sV)
sColor = "rgb({0},{1},{2})".format(int(rgb[0]*255),int(rgb[1]*255),int(rgb[2]*255))
return sColor

#把空品質數轉換成RGB(數值愈高顏色愈深)，我好像用錯色彩模型了
def color_producer(r,min=0,max=30):
cn="white"
cr=0.7
vRange = max-min
vDiff = vRange / 4
if r<=min+vDiff :
cn = colorWVB([360,0,100],[112,100,100],r,min,min+vDiff) #'green'
elif r>min+vDiff and r<=min+vDiff*2:
cn = colorWVB([112,100,100],[58,100,79],r,min+vDiff*1,min+vDiff*2) #'yellow'
elif r>min+vDiff*2 and r<=min+vDiff*3:
cn = colorWVB([58,100,79],[0,100,71],r,min+vDiff*2,min+vDiff*3) #'red'
else:
cn = colorWVB([0,100,71],[291,100,54],r,min+vDiff*3,min+vDiff*4) #'purple'
return [cn,cr]

#以下為主程式
isdigit = re.compile(r'^\d+\.*\d*\$')
localhtml = "taiwan.html"
html = requests.get(pm25_url)
localtime = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
json = jsonpickle.decode(html.text)
#把json檔變成python可用的diction/list
vMin = 999999
vMax = 0
for j in json['records']:
pm2p5 = j[testName]
if isdigit.match(pm2p5) is not None:
vQ = int(pm2p5)
if vQ < vMin :
vMin = vQ
if vQ > vMax :
vMax = vQ

center = (23.5, 121)
m = folium.Map(location=center, zoom_start=8,title="全台空氣品質("+testName+")即時監測圖況")
folium.map.Marker(
location=(23.2,121.4),
icon=folium.DivIcon(
icon_size=(300,36),
icon_anchor=(0,0),
html="<div style='text-align:center;font-size:1.6em;color:red;background-color:yellow;'>全台空氣品質("+testName+")即時監測圖況</div><div style='text-align:center;'>{0}</div>".format(localtime)),

fg = folium.FeatureGroup(name="PM2.5")
vLevel = (vMin if vMin>0 else 1)/(vMax if vMax>0 else 1)

for j in json['records']:
pm2p5 = j[testName]
if j["Latitude"]!= "" and j["Longitude"]!="" and isdigit.match(pm2p5) is not None:
r = int(pm2p5)
cc = color_producer(r,vMin,vMax)
loc = (j["Latitude"],j["Longitude"])
location=loc,
tooltip=str(r)+'\n'+j["SiteName"],
fill_color=cc[0],
color = 'grey',
fill_opacity=cc[1]))

currpath = "/var/www/html/"+localhtml
loc = 'TAIWAN '+testName
title_html = '<div style="z-index:5000;text-align:center;font-size:2.0em;background-color:transparent;position:fixed;top:0px;left:300px;">{}</div>'.format(loc)
m.save(currpath)

#就醬！

``````

samshum iT邦新手 5 級 ‧ 2021-03-31 17:41:44 檢舉

• H從 240(藍色，代表最低) 往 0(紅色，代表最高) 變化
• S 跟 V 都 100

``````def color_producer(rin,minin=0,maxin=30):
cr=0.7
cn = colorWVB(From=[240,100,100],To=[0,100,100],r=rin,min=minin,max=maxin)
return [cn,cr]

``````