前提・実現したいこと
Pythonで複数ページのスクレイピングを行い、CSV出力する処理を作りたいと思っています。
次のページに遷移した後にロード待機を行いたく、
各種情報を参考にしてWebDriverWaitクラスやexpected_conditionsクラスで
指定をするも、有効とならず困っています。
結果、前のページのテーブル情報が読み込まれてCSVに出力されてしまうという状態です。。
time.sleepを使えばうまくいったのですが、どうすれば
ロード待機が上手くできるのでしょうか。
ソースコードの改善ポイントをご教授いただければ幸いです。
よろしくお願い致します。
該当のソースコード
Python
1from selenium import webdriver 2from selenium.webdriver.chrome.options import Options 3from selenium.webdriver.common.action_chains import ActionChains 4from selenium.webdriver.support.ui import WebDriverWait 5from selenium.webdriver.support import expected_conditions as EC 6from selenium.webdriver.common.by import By 7 8options = Options() 9options.add_argument('--headless') 10driver = webdriver.Chrome('C:\python\chromedriver_win32\chromedriver.exe',chrome_options=options) 11driver.get('http://www.ullet.com/search.html#disp/1') 12 13import pandas as pd 14page_source = driver.page_source 15dfs = pd.read_html(page_source) 16 17dfs[1].to_csv("sample.csv", header=False, index=False) 18 19# ページ数 20page = 10 21 22for num in range(2,page): 23 num2 = str(num) 24 driver.get('http://www.ullet.com/search.html#disp/1/page/' + num2) 25# import time 26# time.sleep(0.4) 27 wait = WebDriverWait(driver, 10) 28 wait.until(EC.presence_of_all_elements_located) 29 wait.until(EC.visibility_of_all_elements_located) 30 page_source = driver.page_source 31 dfs = pd.read_html(page_source) 32 dfs = dfs[1].drop(0, axis=0) 33 dfs.to_csv("sample.csv",mode='a', header=False, index=False) 34 35driver.close() 36driver.quit()
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。