【问题标题】:Web-scraping for td datatd 数据的网络抓取
【发布时间】:2014-12-15 20:00:53
【问题描述】:

有人可以向我解释为什么我的代码没有使用 PriorSettle td 吗?我得到了几个月,但无论出于何种原因,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()))

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping


    【解决方案1】:

    您需要等待表格加载。只需添加延迟就可以了:

    driver.get(url)
    
    time.sleep(3)
    
    table = driver.find_element_by_id('quotesFuturesProductTable1')
    ...
    

    打印:

    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
    

    仅供参考,使用 time.sleep() 的隐式超时不是一种可靠且推荐的等待元素的方法。 Selenium 内置了Waits 机制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 2018-06-30
      相关资源
      最近更新 更多