Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.