Beautifulsoup例外列表超出范围 [英] Beautifulsoup Exception list out of range

查看:302
本文介绍了Beautifulsoup例外列表超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用beautifulsoup做到以下几点:结果
部分= soup.findAll('TBODY')[0]

I'm using beautifulsoup to do the following:
section = soup.findAll('tbody')[0]

如何设置变量一样,使用第一个列表项......没有它抛出一个异常: IndexError:列表索引超出范围如果BS4找不到TBODY?

How can set variable like that using the first list item... without it throwing an exception to: IndexError: list index out of range if BS4 can't find tbody?

任何想法?

推荐答案

您可以从的findAll 返回答案,赤它的长度第一:

You can return the answer from findAll and chek it's length first:

x = soup.findAll("tbody")

if x is not None and len(x) > 0:
    section = x[0]

这篇关于Beautifulsoup例外列表超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆