Find a HTML tag that contains certain text
<p>So I am trying to find a particular string in website html source file.</p>

<p>Ex) If I have following html tag</p>

<pre><code>&lt;div class="rev" data="123456789adfdfdfdfadf"&gt;&lt;/div&gt;
</code></pre>

<p>I want to be able to find this particular line that contain <code>div class = "rev"</code> and data that are inside and print out <code>"123456789adfdfdfdfadf"</code></p>

<p>But before I do that, I am just trying to make sure its finding the right tag but I kept getting <code>[]</code> as output</p>

<p>This is my code</p>

<pre><code>import urllib2
from BeautifulSoup import BeautifulSoup
import re
request = urllib2.Request("http://www.adidas.co.uk/nmd_r1-shoes/BB1970.html")
request.add_header("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5")

f = urllib2.urlopen(request)
soup = BeautifulSoup(f)

d = soup.findAll('div', text = re.compile('123456789adfdfdfdfadf'), attrs = {'class' : 'data'})
print d
</code></pre>

<p>You are mixing your data (as attribute) and the text you're looking for.<br>
With the <code>div</code> given, you should find it with:</p>

<pre><code>print [item["data"] 
       for item in soup.find_all('div', {'_class': 'rev'}) 
       if "data" in item.attrs]
</code></pre>

<p>Or, a bit more accurate:</p>

<pre><code>[item['data-bin'] 
 for item in soup.find_all('div', {'_class': 'rev', attrs={'data-bin' : True}})]
</code></pre>

python html python-2.7