【问题标题】:list index out of range scraping列表索引超出范围抓取
【发布时间】:2016-12-29 10:23:03
【问题描述】:
 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()

上面的代码非常好,但是当table[1] 标签不可用时,它给了我IndexError

IndexError: list index out of range

我试过try n catch没有成功

如何设置条件,如果 table[1] 不存在,我应该退出完整循环并寻找下一个变量

感谢帮助

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    Pythonic 方法是使用try/except(但不是if,当然也不是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()
    

    several reasons为什么这种方式最好。

    【讨论】:

    • 非常感谢有几个更改代码工作正常,try n catch 的想法是我正在寻找的,再次感谢
    【解决方案2】:

    findAll 方法返回一个包含所有匹配元素的列表,这意味着如果列表的长度为 0,则无法匹配任何表。通过检查长度是否大于 1,您可以确定至少有两个匹配的表格元素(因为您使用 findAll 找到的第二个元素)。

    通过将结果存储在变量中,您还可以避免重复工作(即调用 findAll 两次)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多