【发布时间】: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