2021/04/18現在利用できません
i.river.go.jpから正規表現で抽出
Twitterの文字数多くなったのでぎりぎりいけそう
import re import time import requests import twitter from bs4 import BeautifulSoup headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko' } # 今治警報 imabari = { 'url': 'http://i.river.go.jp/_-p01-_/p/xmn0501010/?mtm=10&swd=&prf=3801&twn=3801202', 'pattern': r'■河川予警報洪水予警報等(.*?)水防警報(.*?)ダム・堰放流通知(.*?)※河川予警報' } # 玉川ダム tamagawa = { 'url': 'http://i.river.go.jp/_-p01-_/p/ktm1801020/?mtm=10&swd=&prf=3801&twn=3801202&rvr=&den=0972900700006', 'pattern': r'河川名蒼社川所在地今治市更新下戻る(\d{2}/\d{2} \d{2}:\d{2})現在テレメータ流域平均雨量時間雨量(.*?)累加雨量(.*?)全流入量(.*?)全放流量(.*?)貯水位(.*?)貯水量(.*?)貯水率\(対利水容量\)(.*?)貯水率\(対有効容量\)' } # 水位 souja = { 'url': 'http://i.river.go.jp/_-p01-_/p/ktm1201010/?mtm=10&swd=&prf=3801&twn=3801202', 'pattern': r'愛媛県今治市更新下戻る(\d{2}/\d{2} \d{2}:\d{2})の更新情報単位:m■頓田川国分橋\(自\)(.*?)■蒼社川高野\(自\)(.*?)中通\(自\)(.*?)片山\(自\)(.*?)■台本川山田\(自\)(.*?)除川\(自\)(.*?)(\*過去のデータです。)?■近隣の市町村' } # テレメータ雨量 telemeter = { 'url': 'http://i.river.go.jp/_-p01-_/p/ktm0201010/?mtm=10&swd=&prf=3801&twn=3801202', 'pattern': r'愛媛県今治市更新下戻る(\d{2}/\d{2} \d{2}:\d{2})の更新情報時間/累加単位:mm■蒼社川今治\(自\)(.*?)/(.*?)玉川ダム\(自\)(.*?)/(.*?)鈍川\(自\)(.*?)/(.*?)木地・県\(自\)(.*?)/(.*?)片山\(自\)(.*?)/(.*?)■.*?玉川・気\(気\)(.*?)/(.*?)今治・気\(気\)(.*?)/(.*?)西条.*?大西・道\(道\)(.*?)/(.*?)(\*過去のデータです。)?■近隣の市町村' } # スクレピング def scraping(url, pattern): time.sleep(1) r = requests.get(url, headers=headers) if r.status_code == requests.codes.ok: soup = BeautifulSoup(r.content, 'html.parser') body = soup.body.get_text(strip=True).replace('\xa0', '') # print(body) result = [i.strip() for i in re.findall(pattern, body)[0]] return result if __name__ == '__main__': imabari_list = scraping(imabari['url'], imabari['pattern']) tamagawa_list = scraping(tamagawa['url'], tamagawa['pattern']) souja_list = scraping(souja['url'], souja['pattern']) telemeter_list = scraping(telemeter['url'], telemeter['pattern']) # 日時が全部同じか if tamagawa_list[0] == souja_list[0] == telemeter_list[0]: alert = '洪水予警報等:{0[0]}\n水防警報:{0[1]}\nダム・堰放流通知:{0[2]}'.format( imabari_list) dam = '【ダム】\n流入量:{0[3]}\n放流量:{0[4]}\n貯水位:{0[5]}\n貯水率:{0[7]}'.format( tamagawa_list) river = '【水位】\n高野:{0[1]}m\n中通:{0[2]}m\n片山:{0[3]}m'.format(souja_list) rain = '【雨量】\n今治:{0[13]}/{0[14]}mm\n玉川:{0[11]}/{0[12]}mm'.format( telemeter_list) # rain += '\n\n今治:{0[1]}/{0[2]}mm\nダム:{0[3]}/{0[4]}\mmn鈍川:{0[5]}/{0[6]}mm\n木地:{0[7]}/{0[8]}mm\n片山:{0[9]}/{0[10]}mm'.format(telemeter_list) twit = '\n\n'.join( [tamagawa_list[0], alert, dam, river, rain, imabari['url']]) print(twit) print(len(twit)) api = twitter.Api( consumer_key='', consumer_secret='', access_token_key= '', access_token_secret='') status = api.PostUpdate( twit, media='http://www.pref.ehime.jp/kasen/Jpeg/Cam006/00_big.jpg')