SlackBotでスクレイピング結果を取得したい
Qiitaにある、「1日」「週間」「月間」の各トレンド情報を取得したです。
「月間」をBotに投げたところ、月間の記事を取得しました。
ところが、続けて「週間」をBotに投げたところ、月間の記事を取得してしまいました。
同じように、「1日」をBotに投げても、月間の記事を取得してしまいます。
@respond_toには、"1日*" "週間*" "月間*" と宣言しています。
発生している問題・エラーメッセージ
別トレンドの情報を取得できない。
該当のソースコード
from slackbot.bot import respond_to from selenium import webdriver import random import time import sys import os # 結果を格納するリスト item_list = [] class Scrapy(object): def __init__(self): self.driver = webdriver.Chrome() # 結果を入れる self.result = [] def login(self): # ログイン画面のURL login = "https://qiita.com/login?redirect_to=%2F" # user名 user = "QiitaのUser名" # パスワード password = "QiitaのPassword" # ログイン画面を表示 self.driver.get(login) # "id=identity"の値を取得 attribute = self.driver.find_element_by_name("identity") # 記述内容の初期化 attribute.clear() # id="user"として設定 attribute.send_keys(user) # "id=password"の値を取得 attribute = self.driver.find_element_by_name("password") # 記述内容の初期化 attribute.clear() # id="passworf"として設定 attribute.send_keys(password) # name="commit"の値を取得 self.driver.find_element_by_name("commit").click() # --- ログイン完了 --- # return True def get_trend_data(self, url): # ドライバーを用いてURLを開く self.driver.get(url) # timeout処理 WebDriverWait(self.driver, 5).until(EC.presence_of_element_located) # tr-Item_bodyという要素を取得 trends = self.driver.find_elements_by_class_name("tr-Item_body") for trend in trends: # dataをリスト型で保管(後にappendするため) data = [] # aタグの取得 items = trend.find_elements_by_tag_name("a") # itemsからhrefの取得 href = items[0].get_attribute("href") # 0番目に格納されている文字列を取得(タイトル) title = items[0].text data.append(title) data.append(href) self.result.append(data) self.driver.quit() return self.result def common(url, message): # スクレイピングモジュールの呼び出し sc = Scrapy() # ログイン処理 sc.login() # スクレイピング結果の取得 results = sc.get_trend_data(url) # 2次元配列なので、展開してリストに格納 for v in results: for item in v: item_list.append(item) # 格納された結果を取得 for item in item_list: # 結果を入れてあげる message.reply(item) @respond_to('月間*') def mon(message): url = "https://qiita.com/?scope=monthly" common(url, message) @respond_to('週間*') def week(message): url = "https://qiita.com/?scope=weekly" common(url, message) @respond_to('1日*') @respond_to('一日*') def day(message): url = "https://qiita.com" common(url, message)
試したこと
@listen_toを利用したが変わらず。
補足情報(FW/ツールのバージョンなど)
随時更新します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。