Python 美丽的汤找不到标签时出现所有错误

Python 美丽的汤找不到标签时出现所有错误,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,正如标题所说,我需要检查find_all是否返回一个值 第一次尝试。这没有问题 cmslink = 'https://www.cms.gov/research-statistics-data-and-systemsstatistics-trends-and-reportsmcradvpartdenroldatamonthly-pdp/pdp-enrollment-scc-2020-01' content, _ = http_request_get(url=cmslink,payload={'t

正如标题所说,我需要检查find_all是否返回一个值

第一次尝试。这没有问题

cmslink =  'https://www.cms.gov/research-statistics-data-and-systemsstatistics-trends-and-reportsmcradvpartdenroldatamonthly-pdp/pdp-enrollment-scc-2020-01'
content, _ = http_request_get(url=cmslink,payload={'t':''},parse=True)
table = [a['href'] for a in content.find("ul", class_="field__items").find_all('a')]
第二次尝试。此尝试失败,因为页面没有它要查找的链接

cmslink = 'https://www.cms.gov/Research-Statistics-Data-and-Systems/Statistics-Trends-and-Reports/MCRAdvPartDEnrolData/Monthly-Contract-and-Enrollment-Summary-Report-Items/Contract-Summary-2017-04'
content, _ = http_request_get(url=cmslink,payload={'t':''},parse=True)
table = [a['href'] for a in content.find("ul", class_="field__items").find_all('a')]
我的问题是,在执行设置表变量的行之前,如何检查一些方法

我得到的错误并没有多大帮助,我通过检查页面发现链接丢失了。当我在一个没有链接丢失的页面上运行它时,它运行良好

AttributeError: 'NoneType' object has no attribute 'find_all'
正如斯里兰卡建议的那样:

try:
    table = [a['href'] for a in content.find("ul", class_="field__items").find_all('a')]
except AttributeError:
    print( 'No class_="field__items" found')
或:


两种方式。1) 你可以尝试一下,除了。2) 在尝试获取href属性之前,请检查find_all('a')结果的长度是否大于0。确定。同意,但是什么语法我不能运行我的当前行,因为它失败了。我的python语法仍在进行中OK第一个成功了谢谢。第二个不起作用,因为它尝试执行失败
a_list = content.find("ul", class_="field__items")
if len(a_list != 0):
    table = [a['href'] for a in a_list.find_all('a')]