无法找到标签的美丽汤find_all错误

问题描述 投票:0回答:1

正如标题所说,我需要一些方法来检查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'
python web-scraping beautifulsoup
1个回答
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')]
© www.soinside.com 2019 - 2024. All rights reserved.