Last active
January 25, 2021 11:47
-
-
Save shinofara/413c52b38d9ffedb7650fefcebb442af to your computer and use it in GitHub Desktop.
webスクレイピングして、slackに通知
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import urllib2 as request | |
from bs4 import BeautifulSoup | |
import re | |
import urllib | |
import datetime | |
import json | |
import yaml | |
from slack import channel | |
def loadYaml(path): | |
f = open(path, 'r').read() | |
data = yaml.load(f) | |
return data | |
class SlackClient: | |
slackClient = '' | |
def __init__(self, path): | |
yaml = loadYaml(path) | |
self.slackClient = channel.Client(yaml['account'], yaml['token']) | |
def post(self, channel, text): | |
self.slackClient.post(text, channel) | |
SLACK_YAML = './config/slack.yml' | |
def main(): | |
# slack clientを生成する | |
slackClient = SlackClient(SLACK_YAML) | |
# ブログから Content-Body を取得する | |
response = request.urlopen('<URL>') | |
body = response.read() | |
# HTML をパースする | |
soup = BeautifulSoup(body, 'lxml') | |
# CalendarTable | |
divs = soup.find_all('div', {'id': 'hogehoge'}) | |
for div in divs: | |
# <div> の中にある <ul> を取り出す | |
ul = div.ul | |
# <ul>内の<li>分ループ | |
for li in ul: | |
# <li>の中にキーワードを探す | |
if re.compile("KEYWORD").search(str(li)): | |
slackClient.post("#general", "XXXXを見つけました") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
簡単なスクリプトだけど、とある事情で毎日サイトをチェックして、特定の文言が現れたらslackに通知したいなと考えて
さくっと書きました