简体   繁体   English

td 数据的网络抓取

[英]Web-scraping for td data

Can someone explain to me why my code is not picking up the PriorSettle td's?有人可以向我解释为什么我的代码没有选择PriorSettle td 吗? I am getting the months but for whatever reason the PrioSettle column is not showing up.我得到了几个月,但无论出于何种原因,PrioSettle 列都没有出现。

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()))

You need to wait for the table to load.您需要等待表加载。 Simply adding a delay made it work for me:简单地添加延迟使它对我有用:

driver.get(url)

time.sleep(3)

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

Prints:印刷:

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

FYI, implicit timeouts using time.sleep() is not a reliable and recommended way to wait for elements.仅供参考,使用time.sleep()隐式超时不是等待元素的可靠且推荐的方式。 Selenium has built-in Waits mechanism. Selenium具有内置的Waits机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM