2016-04-07 11 views
1

私はこのサイトのデータにアクセスしようとしています:http://surge.srcc.lsu.edu/s1.html これまでのところ、2つのドロップダウンメニューを使ってコードループを行っていましたが、テーブルには動的に名前が付けられていて、そこからデータを取得するのに問題があります。私は上記の "output_data_table"のクラスを介してデータにアクセスしようとしていましたが、問題を抱えていました。Pythonでセレンを使った動的テーブルの掻き取り

# importing libraries 
from selenium import webdriver 
import time 
from selenium.webdriver.support.ui import Select 
import lxml.html 



driver = webdriver.Firefox() 
driver.get("http://surge.srcc.lsu.edu/s1.html") 

# definition for switching frames 
def frame_switch(css_selector): 
    driver.switch_to.frame(driver.find_element_by_css_selector(css_selector)) 

frame_switch("iframe") 

html_source = driver.page_source 
nameSelect = Select(driver.find_element_by_xpath('//select[@id="storm_name"]')) 
stormCount = len(nameSelect.options) 

for i in range(1, stormCount): 
    print("starting loop on option storm " + nameSelect.options[i].text) 
    nameSelect.select_by_index(i) 
    time.sleep(3) 


    yearSelect = Select(driver.find_element_by_xpath('//select[@id="year"]')) 
    yearCount = len(yearSelect.options) 
    for j in range(1, yearCount): 
     print("starting loop on option year " + yearSelect.options[j].text) 
     yearSelect.select_by_index(j) 


     root = lxml.html.fromstring(driver.page_source) 

     #table=driver.find_element_by_id("output_data_table") 

     for row in root.xpath('.//table[@id="output_data_table"]//tr'): 
     # needs dynamic table name 
      cells = row.xpath('.//td/text()') 
      dict_value = {'0th': cells[0], 
        '1st': cells[1], 
        '2nd': cells[2], 
        '3rd': cells[3], 
        '4th': cells[5], 
        '5th': cells[6], 
        '6th': cells[7], 
        '7th': cells[8]} 
      print(dict_value) 
+0

現時点で問題は何ですか?ありがとう。 – alecxe

+0

"root = lxml.html.fromstring(driver.page_source)"を呼び出す前に待たなければならないようです。あなたが待っていなければ、javascriptによって生成されたテーブルなしでhtmlソースを取得します。それの前に "time.sleep(10)"を入れる –

答えて

0

あなたは "ルート= lxml.html.fromstring(driver.page_source)" を呼び出す前に待機しなければならないようです。

あなたが待っていないなら、テーブルがJavaScriptによって生成されていないhtmlソースを入手してください。その前に "time.sleep(10)"を入れてください。

これはテーブルを取得するようです。簡単な例としてBeautifulSoupを使用しました。

from selenium import webdriver 
import time, re 
from selenium.webdriver.support.ui import Select 
import lxml.html 
from bs4 import BeautifulSoup 

driver = webdriver.Firefox() 
driver.get("http://surge.srcc.lsu.edu/s1.html") 

# definition for switching frames 
def frame_switch(css_selector): 
    driver.switch_to.frame(driver.find_element_by_css_selector(css_selector)) 

frame_switch("iframe") 

html_source = driver.page_source 

nameSelect = Select(driver.find_element_by_xpath('//select[@id="storm_name"]')) 
stormCount = len(nameSelect.options) 

for i in range(1, stormCount): 
    print("starting loop on option storm " + nameSelect.options[i].text) 
    nameSelect.select_by_index(i) 
    time.sleep(3) 


    yearSelect = Select(driver.find_element_by_xpath('//select[@id="year"]')) 
    yearCount = len(yearSelect.options) 
    for j in range(1, yearCount): 
     print("starting loop on option year " + yearSelect.options[j].text) 
     yearSelect.select_by_index(j) 


     time.sleep(10) 

     soup = BeautifulSoup(driver.page_source, 'html.parser') 

     # get the needed table body 
     print soup.find_all("tbody", {"class" : re.compile(".*")})[1].prettify() 


     # print out each column 

     get_table = soup.find_all("tbody", {"class" : re.compile(".*")})[1] 
     columns = get_table.find_all("tr") 

     for column in columns: 
      print column.getText() 
関連する問題