Failed to grab dates in a cutomized manner out of a tabular content

前端 未结 3 1293
醉酒成梦
醉酒成梦 2021-01-07 13:34

I\'ve written a script in python in combination with selenium to parse some dates available within a table in a webpage. The table is located under the header NPL Vict

3条回答
  •  滥情空心
    2021-01-07 14:14

    You the issue is that the items is either an idate or an itime. so you are overwriting one of them every time.

    I commented out your excepts, and it prints fine for me:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from bs4 import BeautifulSoup
    
    link = "http://www.oddsportal.com/soccer/australia/npl-victoria/"
    
    def get_content(driver,url):
        driver.get(url)
        idate = ''
        itime = ''
        for items in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"#tournamentTable tr"))):
            try:
                idate = items.find_element_by_css_selector("th span[class^='datet']").text
            #except Exception: idate = ""
            except:
                pass
            try:
                itime = items.find_element_by_css_selector("td.table-time").text
               # print('itime: ',itime)
           # except Exception: itime = ""
            except:
                pass
            if idate !='' and itime !='':
                print(f'{idate}--{itime}')
    
    if __name__ == '__main__':
        driver = webdriver.Chrome()
        wait = WebDriverWait(driver,10)
        try:
            get_content(driver,link)
        finally:
            driver.quit()
    

提交回复
热议问题