正如标题所述,我需要一些方法来检查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'
答案 0 :(得分:1)
如斯里建议的那样:
try:
table = [a['href'] for a in content.find("ul", class_="field__items").find_all('a')]
except AttributeError:
print( 'No class_="field__items" found')
或:
a_list = content.find("ul", class_="field__items")
if len(a_list != 0):
table = [a['href'] for a in a_list.find_all('a')]