I’m trying to check whether a numerical value is found in a table. Why would this code not find the numerical text “699” in this table? The print statement gives a value of “None.”
html = """
<table>
December 31, 1997 1996 1995 1994 1993
Allowance for credit losses--loans 699 773
Allowance for credit losses--
trading assets 285 190
Allowance for credit losses--
other liabilities 13 10
- --------------------------------------------------------------------------------
Total $ 997 $ 973 $ 992 $1,252 $1,324
================================================================================
</table>
"""
soup = BeautifulSoup(''.join(html))
table = soup.find('table')
test = table.find(text='699')
print test
table.find()will search all tags inside the table, but there are no tags inside the table. There is just a string, which happens to be an ASCII table which is in no way formatted as HTML.If you want to use BeautifulSoup to parse the table, you need to convert it into an HTML table first. Otherwise you can use
table.stringto get the string itself and parse that with regex.