title: Beautiful Soup won't let me parse a variable containing HTML
body: <p>I am attempting to pretty print an HTML email which I have stored in a variable, but I keep getting an error from BS4 that says it is expecting a string.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>from bs4 import BeautifulSoup
  import imaplib
  import email
  
  
  mail = imaplib.IMAP4_SSL('imap.gmail.com')
  
  username = raw_input('USERNAME (email):')
  password = raw_input('PASSWORD: ')
  
  try:
      mail.login(username, password)
      print "Logged in as %r !" % username
  except: 
      imaplib.error
      print "Log in failed."
  
  mail.list()
  # Out: list of "folders" aka labels in gmail.
  mail.select("inbox") # connect to inbox.
  
  result, data = mail.uid('search', None, '(FROM "tiffany@e.tiffany.com")')
  latest_email_uid = data[0].split()[1]
  result, data = mail.uid('fetch', latest_email_uid, '(RFC822)')
  raw_email = data[0][1]
  
  email_message = email.message_from_string(raw_email)
  
  print email_message
  
  html = email_message
  soup = BeautifulSoup(html)
  print soup.prettify()
  </code></pre>
  
  <p>Here is the printed HTML email I am working from: <a href="http://pastebin.com/qfAHwkdV" rel="nofollow">http://pastebin.com/qfAHwkdV</a></p>
  
  <p>This is the error I am getting: </p>
  
  <pre><code>Traceback (most recent call last):
    File "tiff.py", line 34, in &lt;module&gt;
      soup = BeautifulSoup(html)
    File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/bs4/__init__.py", line 169, in __init__
      self.builder.prepare_markup(markup, from_encoding))
    File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/bs4/builder/_htmlparser.py", line 139, in prepare_markup
      dammit = UnicodeDammit(markup, try_encodings, is_html=True)
    File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/bs4/dammit.py", line 203, in __init__
      self._detectEncoding(markup, is_html)
    File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/bs4/dammit.py", line 372, in _detectEncoding
      xml_encoding_match = xml_encoding_re.match(xml_data)
  TypeError: expected string or buffer
  </code></pre>
  
  <p>Why am I unable to pss the HTML to a variable to parse with BS4?</p>
  
  <p>Thanks</p>