title: How can I strip comment tags from HTML using BeautifulSoup?
body: <p>I have been playing with BeautifulSoup, which is great. My end goal is to try and just get the text from a page. I am just trying to get the text from the body, with a special case to get the title and/or alt attributes from <code>&lt;a&gt;</code> or <code>&lt;img&gt;</code> tags. </p>
  
  <p>So far I have this <code>EDITED &amp; UPDATED CURRENT CODE</code>:</p>
  
  <pre><code>soup = BeautifulSoup(page)
  comments = soup.findAll(text=lambda text:isinstance(text, Comment))
  [comment.extract() for comment in comments]
  page = ''.join(soup.findAll(text=True))
  page = ' '.join(page.split())
  print page
  </code></pre>
  
  <p>1) What do you suggest the best way for my special case to NOT exclude those attributes from the two tags I listed above? If it is too complex to do this, it isn't as important as doing #2.</p>
  
  <p>2) I would like to strip<code>&lt;!-- --&gt;</code> tags and everything in between them. How would I go about that?</p>
  
  <p><code>QUESTION EDIT</code> @jathanism: Here are some comment tags that I have tried to strip, but remain, even when I use your example</p>
  
  <pre><code>&lt;!-- Begin function popUp(URL) { day = new Date(); id = day.getTime(); eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=300,height=330,left = 774,top = 518');"); } // End --&gt;
  &lt;!-- var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"}); //--&gt; &lt;!-- var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"}); //--&gt; &lt;!-- var whichlink=0 var whichimage=0 var blenddelay=(ie)? document.images.slide.filters[0].duration*1000 : 0 function slideit(){ if (!document.images) return if (ie) document.images.slide.filters[0].apply() document.images.slide.src=imageholder[whichimage].src if (ie) document.images.slide.filters[0].play() whichlink=whichimage whichimage=(whichimage&lt;slideimages.length-1)? whichimage+1 : 0 setTimeout("slideit()",slidespeed+blenddelay) } slideit() //--&gt;
  </code></pre>