Skip to content

Instantly share code, notes, and snippets.

@donchan922
Last active March 3, 2018 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donchan922/f5ad6d907d8cf75969feec179c7597e4 to your computer and use it in GitHub Desktop.
Save donchan922/f5ad6d907d8cf75969feec179c7597e4 to your computer and use it in GitHub Desktop.
import requests
from bs4 import BeautifulSoup
import re
from bottle import route, run
# 試合速報メッセージ作成
def createReportMessage():
# 阪神の試合速報結果URL
target_url = 'https://m.hanshintigers.jp/game/score/'
# 上記URLに対するGET
r = requests.get(target_url)
# HTMLパーサ
soup = BeautifulSoup(r.text, 'lxml')
# 現在のイニング
inning = soup.find(class_='inning').text
# 各チームの点数リスト
teamPointList = []
# 両チームの点数取得
for i in soup.find_all(class_='number'):
teamPointList.append(i.text)
# ホームチーム名
home_team_name = soup.find(class_='l_left').text
# アウェイチーム名
away_team_name = soup.find(class_='l_right').text
# ホームチームの点数
home_team_point = teamPointList[1]
# アウェイチームの点数
away_team_point = teamPointList[0]
# メッセージ生成
message = inning + '\n' + home_team_name + home_team_point + ' - ' + away_team_name + away_team_point
return message
# 試合速報をLine Notifyで通知する
def postLine(message):
url = 'https://notify-api.line.me/api/notify'
headers = {
# ACCESS_TOKENは自身のトークンと置き換えてください
'Authorization': 'Bearer ACCESS_TOKEN'
}
data = [
('message', message)
]
requests.post(url=url, headers=headers, data=data)
# http://localhost:8080/notifyにリクエストすると発火するイベント
@route('/notify')
def notify():
message = createReportMessage()
postLine(message)
# Python Web Server(bottle)の起動
run(host='localhost', port=8080, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment