Slackから投稿を全て取得する
# python3.6
import requests
import json
import datetime
history_url = "https://slack.com/api/conversations.history"
replies_url = "https://slack.com/api/conversations.replies"
token = "xoxp-xxxxxxxxxx-xxxxxxxx-xxxxxxxxxxx-xxxxxxxxxx"
channel_id = "XXXXXXXXX"
def main():
payload = {
"token": token,
"channel": channel_id
}
response = requests.get(history_url, params=payload)
json_data = response.json()
messages = json_data["messages"]
for i in messages:
#print(i)
print("------------------------------------------------------------------------------------")
unix = int(float(i["ts"]))
print(datetime.datetime.fromtimestamp(unix))
#print(i["user"])
print(i["text"])
payload2 = {
"token" : token,
"channel" : channel_id,
"ts" : i["ts"]
}
response2 = requests.get(replies_url, params=payload2)
json_data2 = response2.json()
messages2 = json_data2["messages"]
cnt = 0
for j in messages2:
if cnt == 0:
cnt += 1
else:
print("--->" + j["text"])
if __name__ == '__main__':
main()
BeautifulSoupを使ったWEBスクレイピング
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import requests
import urllib3
from urllib3.exceptions import InsecureRequestWarning
from bs4 import BeautifulSoup
from datetime import datetime, date, timedelta
from dateutil.relativedelta import relativedelta
ID='ID'
PW='password'
# 今日を取得
today = datetime.today()
one_month_ago = today - relativedelta(months=1)
year = datetime.strftime(one_month_ago, '%Y')
month = datetime.strftime(one_month_ago, '%m')
urllib3.disable_warnings(InsecureRequestWarning)
response = requests.get("https://localhost/auth/login", verify=False)
cookies = response.cookies
#print (response.text)
soup = BeautifulSoup(response.text, "html.parser")
tokentag = soup.input
token = tokentag['value']
#print(cookies)
#print(token)
#print(PW)
response = requests.post(
'https://localhostauth/auth',
{'_token':token, 'empId':ID, 'empPassword':PW},
cookies=cookies,
verify=False)
#print(response.text)
soup = BeautifulSoup(response.text, "html.parser")
tokentag = soup.input
token = tokentag['value']
response = requests.post("https://localhost/fix/disp",
{'_token':token ,'theMonth_Year':year, 'theMonth_Month':month},
cookies=cookies,
verify=False)
#print(response.text)
soup = BeautifulSoup(response.text, "html.parser")
#elems = soup.select('.noBorder')
#rows = elems.findAll('tr')
table = soup.find_all("table")[4]
row = table.findAll('tr')[2]
col = row.findAll('td')[0].string
seleniumを使ったWEBスクレイピング
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
import requests
import time
import os
import csv
# ブラウザからプリンターに接続してジョブ履歴CSVファイルを取得する
userid = 'user'
userpasswd = 'password'
options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
session = requests.session()
driver = webdriver.Chrome(executable_path='/usr/sbin/chromedriver',chrome_options=options)
#driver = webdriver.Chrome(chrome_options=options)
#マウスオーバーを使うための宣言みたいなもの
actions = ActionChains(driver)
# ヘッドレスChromeでファイルダウンロードする
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
driver.execute("send_command", {
'cmd': 'Page.setDownloadBehavior',
'params': {
'behavior': 'allow',
'downloadPath': homedir
}
})
#driver = webdriver.Chrome(chrome_options=options)
url = "http://localhost:8000"
driver.get(url)
#print(driver.title)
#print(driver.page_source)
# ID/PASSを入力
username = driver.find_element_by_id("USERNAME")
username.send_keys(userid)
password = driver.find_element_by_id("PASSWORD_T")
password.send_keys(userpasswd)
#sfile = driver.get_screenshot_as_file("C:\\Users\\yarakawa\\Desktop\\selenium\\File01.png")
# ボタンをクリック
login_button = driver.find_element_by_name("LoginButton")
login_button.click()
# ボタンをクリック
driver.implicitly_wait(10)
standby = driver.find_element_by_class_name("App1_5")
standby.click()
# CSV取得
driver.implicitly_wait(20)
driver.execute_script("get_loglist()")
time.sleep(10)
#driver.close()
driver.quit()