最近熱門的藥局領口罩, 有公開的CSV可以下載, 用python簡單就可抓取了, 觀察了一下資料,更新速度還蠻快的, 但是有住址而無GPS座標, 如果要用地圖, 需要一個轉換, 一次轉換那麼多點要花很多錢, 最好是用cache的方式, 有抓到就先存起來, 要查詢時先去cache找, 沒有再去查詢.

https://github.com/googlemaps/google-maps-services-python 有個 python 的 google map API, 試了一下還可以, 但是geocode服務找到的GPS座標, 跟直接用瀏覽器在 google map 找的座標有點誤差, 不知原因為何? 這個 package, 如果要顯示中文, 要自己加 language='zh-TW', 以下是原範例改寫的例子:

# -*- coding: utf-8 -*-
"""
https://github.com/googlemaps/google-maps-services-python
"""   
 
import googlemaps
from datetime import datetime
 
gmaps = googlemaps.Client(key='自己的key')
 
# Geocoding an address
geocode_result = gmaps.geocode('總統府', language='zh-TW')
 
# Look up an address with reverse geocoding
reverse_geocode_result = gmaps.reverse_geocode((23.013, 120.260), language='zh-TW')
......
 

 

口罩存量查詢:

import csv
import requests
import googlemaps
 
url = "https://data.nhi.gov.tw/resource/mask/maskdata.csv"
gmaps = googlemaps.Client(key='自己的key')
 
with requests.Session() as s:
    download = s.get(url)
    decoded_content = download.content.decode('utf-8')
    cr = csv.reader(decoded_content.splitlines(), delimiter=',')
    mask_list = list(cr)
    for row in mask_list:
        if ("臺南市永康區大灣路" in row[2] and int(row[4])>0):
            geocode = gmaps.geocode(row[2], language='zh-TW')
            lat = geocode[0]['geometry']['location']['lat']
            lng = geocode[0]['geometry']['location']['lng']
            #lat = ''
            #lng = ''
            print("{0:>5d} {1:\u3000<9} {2:\u3000<28} {3:\u3000<12} {4} {5:.7f} {6:.7f}".format\
                  (int(row[4]), row[1], row[2], row[3], row[6], lat, lng))


結果:

比對 google map 查詢結果, 以怡品藥局為例, geocode 查詢是 (22.9998360 120.2564250), google map 顯示為 (22.9976238,120.2590053)

 










 

文章標籤
全站熱搜
創作者介紹
創作者 ghostyguo 的頭像
ghostyguo

No More Codes

ghostyguo 發表在 痞客邦 留言(0) 人氣(791)