count certain words on html file by python
<p>I am a rookie in Python. I am trying to count some words or expressions on html files. For example,I have a piece of html with source codes as below:</p>

<pre><code>&lt;div style="line-height:120%;text-align:justify;text-indent:24px;font-size:10.5pt;"&gt;
&lt;font style="font-family:inherit;font-size:10.5pt;font-style:italic;font-weight:bold;"&gt;2013 vs. 2012&amp;#160;&amp;#160;&lt;/font&gt;
&lt;font style="font-family:inherit;font-size:10.5pt;"&gt;During 2013, the Company recognized a decommissioning charge of $117 million and a restoration liability of $50 million, partially offset by the 2013 reversal of the $56&amp;#160;million tax indemnification liability associated with the 2006 sale of the Company&amp;#8217;s Canadian subsidiary.&lt;/font&gt;&lt;/div&gt;
</code></pre>

<p>I want to count how many times "liability" show up in the piece. Below is my  code, which is not working:</p>

<pre><code>import os
from bs4 import BeautifulSoup

lst=os.listdir("C:/html/")
for x in lst:
    print (x)
    html = open ("C:/html/"+x,'rb')
    bsobj = BeautifulSoup(html,"html.parser")
    metricslist = bsobj.findAll(div.string ='liability')
    print(len(metricslist)) 
</code></pre>

<p>I know bsobj.findAll(div.string ='liability') is very wrong, but have no idea on what the code should be. Any help will be appreciated!</p>

<p>You can apply a <em>partial string match</em> on an element's text when using <code>find()</code> or <code>find_all()</code>:</p>

<pre><code>soup.find(text=lambda text: text and "liability" in text)
</code></pre>

<p>Or, a <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#a-regular-expression" rel="nofollow">regular expression pattern</a> can be used in place of a <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#a-function" rel="nofollow">function</a>:</p>

<pre><code>soup.find(text=re.compile(r"\bliability\b")
</code></pre>

python beautifulsoup