Skip to content

Instantly share code, notes, and snippets.

@shinofara
Last active January 25, 2021 11:47
Show Gist options
  • Save shinofara/413c52b38d9ffedb7650fefcebb442af to your computer and use it in GitHub Desktop.
Save shinofara/413c52b38d9ffedb7650fefcebb442af to your computer and use it in GitHub Desktop.
webスクレイピングして、slackに通知
#!/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()
@shinofara
Copy link
Author

L56 liのテキストが KEYWORD にマッチするかどうか

@shinofara
Copy link
Author

L57 指定したチャンネルにテキストをポストします。

@shinofara
Copy link
Author

簡単なスクリプトだけど、とある事情で毎日サイトをチェックして、特定の文言が現れたらslackに通知したいなと考えて
さくっと書きました

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment