Skip to content

Instantly share code, notes, and snippets.

@kdmgs110
Last active September 23, 2017 22:36
Show Gist options
  • Save kdmgs110/221c1905dbf9ba16443e7af77fdbc8ca to your computer and use it in GitHub Desktop.
Save kdmgs110/221c1905dbf9ba16443e7af77fdbc8ca to your computer and use it in GitHub Desktop.
Slack.py
from selenium import webdriver
import time
import requests # for slack
import json # for slack
#Phanttomjs Driverを入手
browser = webdriver.PhantomJS('/usr/local/bin/phantomjs') # DO NOT FORGET to set path
#TODO set search query
query = "ワンピース" # set search query here
url = "http://okepi.net/top.aspx"
#TODO access to targeted URL
print("Successfully accessed to the targeted page! title:{}".format(browser.title))
#TODO insert query into #txtTopSearch
queryField = browser.find_element_by_xpath("//*[@id='txtTopSearch']") # This x-path locates in search form
queryField.send_keys(query) #Insert query message into search form
#TODO submit and load page
submitButton = browser.find_element_by_xpath('//*[@id="btnTopSearch"]')
submitButton.click()
browser.implicitly_wait(5) # seconds
print("Submit completed. now at:{}".format(browser.title))
#TODO retrieve ticket data
tables = browser.find_elements_by_class_name("tbl_j") #<table class = "tbl_j">
print("Number of table:{}".format(len(tables)))
#TODO insert data that you want from http://okepi.net/all.aspx?key=performance&value=%83%8F%83%93%83s%81[%83X
contents = [] #Stores each content later
for table in tables:
try:
print("#######################")
title = table.find_element_by_class_name("goleft") #without this, title retrieves web-element object
print("title:{}".format(title.text))
titleURL = title.find_element_by_css_selector("a").get_attribute("href")
print("titleURL:{} ".format(titleURL))
datetime = table.find_element_by_class_name("pdatetime").text
print("datetime:{} ".format(datetime))
price_place = table.find_element_by_class_name("price").text
print("price_place:{} ".format(price_place))
#TODO make summary of each data
content = title.text + "\n" + datetime + "\n" + price_place + "\n" + titleURL + "\n" + "###################\n"
print(content)
contents.append(content) # insert contents so slack can post only once
except Exception as e: #error message appears when something goes wrong
print(e)
continue
#TODO create summary for slack
summary = "==============" + query + "の新着情報!================\n" #
for content in contents:
summary = summary + content
print(summary) # This summary is sent to slack finally
#TODO post summary on slack
slackURL = "[your slack app URL]" # https://api.slack.com/custom-integrations
requests.post(slackURL, data = json.dumps({
'text': summary, # text that you will post
'username': u'Musical Reminder', # username (whatever name you want is fine)
'icon_emoji': u':ghost:', # profile emoji
'link_names': 1, # enables mention
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment