python - 编辑 BS4,当网页中没有表格时不返回任何内容

标签 python html beautifulsoup

当 html 页面中包含文本 “内容逻辑定义” 的标记 h2 时,以下代码有效,例如 https://www.hl7.org/fhir/valueset-account-status.html

def extract_table(url):
                r = requests.get(url)
                soup = BeautifulSoup(r.content, 'lxml')
                h2 = soup.find(lambda elm: elm.name == 'h2' and 'Content Logical   Definition' in elm.text)
                div = h2.find_next_sibling('div')
                return div.find('table')

但是对于以下不包含h2的网页,带有`“内容逻辑定义”,例如https://www.hl7.org/fhir/valueset-cpt-all.html返回以下错误:

'NoneType' object has no attribute 'find_next_sibling'

当网页中没有带有内容逻辑定义的h2时,如何编辑代码以返回非表。

最佳答案

您可以通过两种常见方式来完成此操作:

  • LBYL - look before you leap :

    h2 = soup.find(lambda elm: elm.name == 'h2' and 'Content Logical   Definition' in elm.text)
    return div.find_next_sibling('div').find('table') if h2 else None
    
  • EAFP - easier to ask for forgiveness than permission :

    try:
        h2 = soup.find(lambda elm: elm.name == 'h2' and 'Content Logical   Definition' in elm.text)
        div = h2.find_next_sibling('div')
        return div.find('table')
    except AttributeError:
        return None
    

关于python - 编辑 BS4,当网页中没有表格时不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37643689/

相关文章:

python中缀前向管道

python - Keras 加载彩色图像

python - 填写缺失的日期

python - 使用python从HTML页面获取数据

python - 脚本在抓取多个值时抛出错误

python - 在 BeautifulSoup 中查找不同的字符串并返回包含标签

python - Python 的 "from <module> import <symbol>"的动态等价物

html - TablePress 分页

html - 使用 flexbox 制作水平条形图 - 宽度未按预期工作

javascript - 将值提交到另一个页面 iframe 表单