2014-10-20 60 views
1

有人可以向我解釋爲什麼我的代碼沒有選擇PriorSettle td嗎?我得到了幾個月,但無論出於何種原因,PrioSettle列都沒有顯示出來。td數據的網頁報廢

lc_result={} 

url = "http://www.cmegroup.com/trading/agricultural/livestock/live-cattle.html" 

driver = webdriver.Chrome() 
driver.set_window_size(2,2) 
driver.get(url) #this will go the the actual url listed 
print('  Live Cattle Futures'+localtime.center(50)) 
table = driver.find_element_by_id('quotesFuturesProductTable1') 
for row in table.find_elements_by_tag_name('tr')[2:]: 
    month=row.find_elements_by_tag_name('td')[0].text 
    priorsettle=row.find_elements_by_tag_name('td')[4].text 

    print month, priorsettle 
    lc_result[month]=[priorsettle] 

driver.close() 
print(str(date.today())) 

回答

1

您需要等待表加載。簡單地增加一個延遲使得它爲我工作:

driver.get(url) 

time.sleep(3) 

table = driver.find_element_by_id('quotesFuturesProductTable1') 
... 

打印:僅供參考,使用time.sleep()隱性超時

DEC 2014 168.025 
FEB 2015 166.900 
APR 2015 164.775 
JUN 2015 154.800 
AUG 2015 152.900 
OCT 2015 154.100 
DEC 2015 154.250 
FEB 2016 153.850 
APR 2016 0.000 

不是一個可靠的和推薦的方式來等待元素。 Selenium已內置Waits機制。

+0

非常感謝alecxe。原代碼工作。 – Riggs 2014-10-21 00:13:38