0
 soup = BeautifulSoup(driver.page_source)

        for each_div in soup.findAll("div", { "class" : "trapac-form-view-results tpc-results" }):

            if each_div.findAll("table", {"class": "sticky-enabled table-select-processed tableheader-processed sticky-table"})[1]:
                for child0 in each_div.findAll("table", {"class": "sticky-enabled table-select-processed tableheader-processed sticky-table"})[1]:

                    if child0.name == "table":
                         print("2")
                         child = child0.findChildren()

                         for child in child0:
                             if child.name == "tbody":
                                 child1 = child.findChildren()

the code above perfectly fine but when the table[1] tag is not available it gives me IndexError

IndexError: list index out of range

i tried try n catch not succeeded

how i can put condition in such a way that if table[1] does not exist i should be out of complete loop and look for next variable

help appreciated

宏杰李
  • 11,820
  • 2
  • 28
  • 35
GAURAV CHHABRA
  • 87
  • 2
  • 2
  • 9

2 Answers2

0

The method findAll returns a list containing all matching elements, meaning that if the length of the list is 0 then no tables could be matched. By checking that the length is greater than 1 you can be certain that there's at least two table elements that matched (since you use that second element found by findAll).

By storing the result in a variable you can also avoid doing duplicate work (i.e. calling findAll twice).

soup = BeautifulSoup(driver.page_source)

for each_div in soup.findAll("div", { "class" : "trapac-form-view-results tpc-results" }):
    tables = each_div.findAll("table", {"class": "sticky-enabled table-select-processed tableheader-processed sticky-table"})
    if len(tables) > 1:
        for child0 in tables[1]:

            if child0.name == "table":
                 print("2")
                 child = child0.findChildren()

                 for child in child0:
                     if child.name == "tbody":
                         child1 = child.findChildren()
ToJa92
  • 431
  • 4
  • 8
0

Pythonic way to do this is using try/except (but not if and of course not len!)

soup = BeautifulSoup(driver.page_source)

    for each_div in soup.findAll("div", { "class" : "trapac-form-view-results tpc-results" }):

        try:
            node = each_div.findAll("table", {"class": "sticky-enabled table-select-processed tableheader-processed sticky-table"})[1]
        except IndexError:
            continue

        for child0 in node:

            if child0.name == "table":
                 print("2")
                 child = child0.findChildren()

                 for child in child0:
                     if child.name == "tbody":
                         child1 = child.findChildren()

There are several reasons why this way is the best one.

Community
  • 1
  • 1
Dmitry
  • 2,026
  • 1
  • 18
  • 22