2017-04-03 71 views
1

我想解析網站的利率,但我不能從<td>元素中取出數據。蟒蛇刮阿賈克斯內容

我寫的短代碼來測試它得到第1行數據TABEL:

from selenium import webdriver 

driver = webdriver.Firefox() 
driver.get('https://www.gpw.pl/wskazniki_spolek_full') 
table = driver.find_elements_by_xpath("//table[@class='tab03']/tbody/tr")[4].text 
print table 

driver.quit() 

和我得到的結果:

2 PLNFI0800016 141 08OCTAVA 42 786 848 44,07 63,86 2016-12-31 H 0,69 27,80 ---

,但我想通過所有<td><tr>中的元素標記在所有表中,具有class='tab03'

table = driver.find_elements_by_xpath("//table[@class='tab03']/tbody/tr") 

for el in table: 
    col_id = el.find_element_by_tag_name('td')[1].text 
    col_kod = el.find_element_by_tag_name('td')[2].text 

    print("{}".format(col_id, col_kod)) 

driver.quit() 

,但我得到的錯誤:selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: td

回答

1

有沒有在他們裏面td元素一些標題行,跳過它們:

rows = driver.find_elements_by_xpath("//table[@class='tab03']/tbody/tr") 

for row in rows[3:]: 
    cells = row.find_elements_by_tag_name('td') 
    col_id = cells[0].text 
    col_kod = cells[1].text 

    print("{}".format(col_id, col_kod)) 

還要注意的是,去的td細胞,請使用find_elements_by_tag_name()並通過索引(基於0)獲取所需的元素。