環境
windows10
python 3.7.9 64bit
実現させたい事
redditというサイトにて、猫のサブレディット(猫のコミュニティ)の画面でスクロールさせた状態でいいねの数を取得できればと考えております。
やったこと
実際のコードです。冗長ではありますが見て頂けると幸いです。
python
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from webdriver_manager.chrome import ChromeDriverManager import time chrome_path = r"C:\Users\hoge\Downloads\chromedriver_win32 (4)\chromedriver.exe" options = Options() options.add_argument("--headless") options.add_experimental_option("excludeSwitches", ['enable-automation']) driver = webdriver.Chrome(ChromeDriverManager().install(), options = options) capabilities = DesiredCapabilities.CHROME.copy() capabilities["acceptInsecureCerts"] = True url = "https://www.reddit.com/r/cats/new/" def change_view(): driver.get(url) # 表示変更 change_view_xpath = '//*[@id="LayoutSwitch--picker"]' button = driver.find_element_by_xpath(change_view_xpath) button.click() time.sleep(2) for i in range(30): try: change_view_xpath_span = "/html/body/div[{}]/div/button[3]".format(i) button_second = driver.find_element_by_xpath(change_view_xpath_span) button_second.click() time.sleep(3) break except: pass def scrolling(): SCROLL_PAUSE_TIME = 0.5 # Get scroll height last_height = driver.execute_script("return document.body.scrollHeight") n = 0 while True: # Scroll down to bottom driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # Wait to load page time.sleep(SCROLL_PAUSE_TIME) # Calculate new scroll height and compare with last scroll height new_height = driver.execute_script("return document.body.scrollHeight") if new_height == last_height: n += 1 if n == 10: break if new_height >= 10000: break else: print(new_height) n=0 last_height = new_height time.sleep(5) def get_score(): score_lis = [] scoer_class = "_1rZYMD_4xY3gRcSS3p8ODO._25IkBM0rRUqWX5ZojEMAFQ" score_elems = driver.find_elements_by_class_name(scoer_class) for score_elem in score_elems: score = score_elem.text score_lis.append(score) print(str(len(score_elems)), " 取得できたscoreの数") print(score_lis) change_view() scrolling() get_score()
こちらを実行すると猫のサブレディットで少しスクロールした後にいいねの数を取得していくのですが、実行結果は以下になります。
175 取得できたscoreの数 ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '13', '39', '8', '5', '55', '0', '2', '43', '0', '33', '21', '14', '19', '23', '13', '5', '50', '32', '13', '17', '18', '19', '17', '1', '24', '39', '9', '15', '13', '27', '31', '21', '13', '17', '15', '15', '23', '19', '11', '10', '20', '19', '45', '14', '1', '9', '16', '12', '8', '49', '27', '17', '14', '20', '15', '7', '18', '25', '17', '12', '15', '1', '35', '17', '24', '20', '29', '16', '27', '26', '2', '2']
調べた事
上のコードでスクロールさせずに(scrolling関数を止めて)実行させると、実行結果は以下の様になります。
25 取得できたscoreの数 ['Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote', 'Vote']
最初にスクロールさせて実行した際に空で返ってきた部分が、スクロールさせないとちゃんとテキストとして取得できます。
スクロールさせた後にすべての投稿のURLを取得して、それぞれのページにあるいいねの数を取得する方法はいけるかな思いましたが、他に良い解決策があるのでは?と思ったのと
python scraping reddit good score
などでググったりしてみたのですが、参考にできそうなサイトが見つけられずこちらで質問させていただきました。
質問
どうしたらスクロールさせたうえで、全てのいいねの数をテキストとして取得できますでしょうか??
有識者様のお力をお借りできればと思います。
全ての投稿のURLを取得して、投稿それぞれのページに載っているいいね数を取得する方法しかなさそうであれば、またやり直してみたいと思います。
まだ回答がついていません
会員登録して回答してみよう