Skip to content Skip to sidebar Skip to footer

Getting Table Value From Nowgoal Has Got An Index Error

I am quite new to scraping. I am getting links from nowgoal. Below is how I started navigating to above page. I do not wish to get link for all matches. But I will have an input te

Solution 1:

league_list = league_list = ["English Premier League", 'Italian Serie A',
                             'England Championship', 'Spanish La Liga', 'Swedish Allsvenskan', 'USA Major League Soccer','Saudi','Dutch Cup']
#wait for some time# wait for some time
wait.until(EC.element_to_be_clickable((By.ID, "li_league"))).click()
# click on the -team ranking
wait.until(EC.element_to_be_clickable(
    (By.XPATH, "//label[@for='TeamOrderCheck']/span"))).click()

for league in league_list:
    try:
        nextRow = wait.until(EC.presence_of_element_located(
            (By.XPATH, '//tr[.//a[contains(text(),"{}")]]'.format(league))))
        id = nextRow.get_attribute("id").split("_")[1]
        try:

            row = wait.until(EC.presence_of_all_elements_located(
                (By.XPATH, '//tr[preceding-sibling::tr[.//a[contains(text(),"{}")]] and following-sibling::tr[@id="tr_{}"] and not(@style="display:none")]'.format(league, int(id)+1))))
            print("########The result for {} ########".format(league))
            for i in row:
                print(i.get_attribute("textContent"))
            print("###########Completed##############".format(league))
        except:
            row = wait.until(EC.presence_of_all_elements_located(
                (By.XPATH, '//tr[preceding-sibling::tr[.//a[contains(text(),"{}")]] and not(@style="display:none")]'.format(league))))
            print("########The result for {} ########".format(league))
            for i in row:
                print(i.get_attribute("textContent"))
            print("###########Completed##############".format(league))
            continueexcept:
        continue

you can use following and preceeding property , as there is no unique way to identify next following element we have to take id and increment it with 1

Solution 2:

Prints all the information of each row.

wait = WebDriverWait(driver, 5)
driver.get('http://www.nowgoal3.com/football/fixture/?type=&f=sc1&date=2021-01-29')
league_list=["English Premier League",'Italian Serie A','England Championship','Spanish La Liga', 'Swedish Allsvenskan','USA Major League Soccer','Swiss Challenge League']
#wait for some time
wait.until(EC.element_to_be_clickable((By.ID, "li_league"))).click()
#click on the -team ranking
wait.until(EC.element_to_be_clickable((By.XPATH, "//label[@for='TeamOrderCheck']/span"))).click()

for league in league_list:
    try:  
        header = driver.find_element_by_xpath("//tr[@class='Leaguestitle fbHead']/td[2]/span/a[text()='"+league+"']")
        #print(len(header))print(header.text)
        nextRow = wait.until(EC.presence_of_element_located(
            (By.XPATH, '//tr[.//a[contains(text(),"{}")]]'.format(league))))
        id = nextRow.get_attribute("id").split("_")[1]
        try:

            rows = wait.until(EC.presence_of_all_elements_located(
                (By.XPATH, '//tr[preceding-sibling::tr[.//a[contains(text(),"{}")]] and following-sibling::tr[@id="tr_{}"] and not(@style="display:none")]'.format(league, int(id)+1))))
     
        except:
            rows = wait.until(EC.presence_of_all_elements_located(
                (By.XPATH, '//tr[preceding-sibling::tr[.//a[contains(text(),"{}")]] and not(@style="display:none")]'.format(league))))

            continue#print(len(rows))for row in rows:
            home = row.find_element_by_css_selector("td:nth-child(5) > a").text
            homeRank = row.find_element_by_css_selector("td:nth-child(5)  span.team-hg").text.strip('[]') 
            away = row.find_element_by_css_selector("td:nth-child(7) > a").text
            awayRank = row.find_element_by_css_selector("td:nth-child(7)  span.team-hg").text.strip('[]')
            link = row.find_element_by_css_selector("td.toolimg >a:nth-child(3)").get_attribute('href')
            link = ''.join(filter(lambda i: i.isdigit(), link))
            link  = 'http://data.nowgoal.group/3in1odds/'+link+'.html'print(home,homeRank,away,awayRank,link)
    except:
        continue

Post a Comment for "Getting Table Value From Nowgoal Has Got An Index Error"