title: Trying to get encoding from a webpage Python and BeautifulSoup
body: <p>Im trying to retrieve the charset from a webpage(this will change all the time). At the moment Im using beautifulSoup to parse the page and then extract the charset from the header. This was working fine until I ran into a site that had.....</p>
  
  <pre><code>    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
  </code></pre>
  
  <p>My code up until now and which was working with other pages is:</p>
  
  <pre><code>    def get_encoding(soup):
          encod = soup.meta.get('charset')
          if encod == None:
              encod = soup.meta.get('content-type')
              if encod == None:
                  encod = soup.meta.get('content')
      return encod
  </code></pre>
  
  <p>Would anyone have a good idea about how to add to this code to retrieve the charset from the above example. Would tokenizing it and trying to retrieve the charset that way be an idea? and how would you go about it without having to change the whole function? 
  Right now the above code is returning "text/html; charset=utf-8" which is causing a LookupError because this is an unknown encoding.</p>
  
  <p>Thanks</p>
  
  <p>The final code that I ended up using:</p>
  
  <pre><code>    def get_encoding(soup):
          encod = soup.meta.get('charset')
          if encod == None:
              encod = soup.meta.get('content-type')
              if encod == None:
                  content = soup.meta.get('content')
                  match = re.search('charset=(.*)', content)
                  if match:
                      encod = match.group(1)
                  else:
                      dic_of_possible_encodings = chardet.detect(unicode(soup))
                      encod = dic_of_possible_encodings['encoding'] 
      return encod
  </code></pre>