如何使用BS4检测页面上没有table数据

How to use BS4 to detect no table data on page

我无法使用 BS4 解析此 HTML table。有时页面没有支付数据,会显示 "There is no pending manifest payment"。其他时候,该页面将列出所有到期的未决付款。我想将此数据输出到一个数组中。

def find_payment(html):
    soup = BeautifulSoup(html)
    table = soup.find('table', cellspacing="0", cellpadding="2", border="0")
    table_body = table.find('tbody')
    rows = table.body.find_all('tr')
    payment_data = []
    for row in rows:
        cols = row.find_all('td')
        cols = [ele.text.strip() for ele in cols]
        account_data.append([ele for ele in cols if ele])
    return payment_data

大部分都解决了。我做了这样的事情:

def find_payment(html):
    soup = BeautifulSoup(html)
    if soup.find(text="There is no pending manifest payment") is not None:
        payment_data.append([0, ID[i]])
    else:
        amount = soup.find('td', {'class': 'bodytext'}, width="35%")
        payment_data.append([amount.text, ID[i]])
    return payment_data

一个选择是有点防御(有点LBYL风格)并预先搜索"There is no pending manifest payment"元素:

if soup.find(text="There is no pending manifest payment") is not None:
    print("No payment data")

为什么不直接查找 "td" class 成功body10

 def find_payments(html):    
        soup = BeautifulSoup(html)
        if soup.find("td", {"class":"success"}):
            payments = "There is no pending manifest payment"
        else:
            payments = [pmnt.text for pmnt in soup.findAll("td", {"class":"body10"})]