title: Beautiful Soup and searching in results
body: <p>These are my first steps with python, please bear with me.</p>
  
  <p>Basically I want to parse a Table of Contents from a single Dokuwiki page with Beautiful Soup. The TOC looks like this:</p>
  
  <pre><code>&lt;div id="dw__toc"&gt;
  &lt;h3 class="toggle"&gt;Table of Contents&lt;/h3&gt;
  &lt;div&gt;
  
  &lt;ul class="toc"&gt;
  &lt;li class="level1"&gt;&lt;div class="li"&gt;&lt;a href="#section"&gt;#&lt;/a&gt;&lt;/div&gt;
  &lt;ul class="toc"&gt;
  &lt;li class="level2"&gt;&lt;div class="li"&gt;&lt;a href="#link1"&gt;One&lt;/a&gt;&lt;/div&gt;&lt;/li&gt;
  &lt;li class="level2"&gt;&lt;div class="li"&gt;&lt;a href="#link2"&gt;Two&lt;/a&gt;&lt;/div&gt;&lt;/li&gt;
  &lt;li class="level2"&gt;&lt;div class="li"&gt;&lt;a href="#link3"&gt;Three&lt;/a&gt;&lt;/div&gt;&lt;/li&gt;
  </code></pre>
  
  <p>I would like to be able to search in the content of the a-tags and if a result is found return its content and also return the href-link. So if I search for "one" the result should be</p>
  
  <pre><code>One
  #link1
  </code></pre>
  
  <p>What I have done so far:</p>
  
  <pre><code>#!/usr/bin/python2
  
  from BeautifulSoup import BeautifulSoup
  import urllib2
  
  
  #Grab and open URL, create BeatifulSoup object
  url = "http://www.somewiki.at/wiki/doku.php"
  page = urllib2.urlopen(url)
  soup = BeautifulSoup(page.read())
  #Grab Table of Contents
  grab_toc = soup.find('div', {"id":"dw__toc"})
  
  #Look for all divs with class: li
  ftext = grab_toc.findAll('div', {"class":"li"})
  #Look for links
  links = grab_toc.findAll('a',href=True)
  
  #Iterate
  for everytext in ftext:
      text = ''.join(everytext.findAll(text=True))
      data = text.strip()
      print data
  
  for everylink in links:
      print everylink['href']
  </code></pre>
  
  <p>This prints out the data I want but I'm kind of lost to rewrite it to be able to search within the result and only return the searchterm. Tried something like</p>
  
  <pre><code>if data == 'searchtearm':
          print data
          break
  else:
          print 'Nothing found'
  </code></pre>
  
  <p>But this is kind of a weak search. Is there a nicer way to do this? In my example the Beatiful Soup resultset is changed into a list. Is it better to search in the result set in the first place, if so then how to do this?</p>