views:

700

answers:

4

Problem

When screen-scraping a webpage using python one has to know the character encoding of the page. If you get the character encoding wrong than your output will be messed up.

People usually use some rudimentary technique to detect the encoding. They either use the charset from the header or the charset defined in the meta tag or they use an encoding detector (which does not care about meta tags or headers). By using only one these techniques, sometimes you will not get the same result as you would in a browser.

Browsers do it this way:

  • Meta tags always takes precedence (or xml definition)
  • Encoding defined in the header is used when there is no charset defined in a meta tag
  • If the encoding is not defined at all, than it is time for encoding detection.

(Well... at least that is the way I believe most browsers do it. Documentation is really scarce.)

What I'm looking for is a library that can decide the character set of a page the way a browser would. I'm sure I'm not the first who needs a proper solution to this problem.

Solution (I have not tried it yet...)

According to Beautiful Soup's documentation.

Beautiful Soup tries the following encodings, in order of priority, to turn your document into Unicode:

  • An encoding you pass in as the fromEncoding argument to the soup constructor.
  • An encoding discovered in the document itself: for instance, in an XML declaration or (for HTML documents) an http-equiv META tag. If Beautiful Soup finds this kind of encoding within the document, it parses the document again from the beginning and gives the new encoding a try. The only exception is if you explicitly specified an encoding, and that encoding actually worked: then it will ignore any encoding it finds in the document.
  • An encoding sniffed by looking at the first few bytes of the file. If an encoding is detected at this stage, it will be one of the UTF-* encodings, EBCDIC, or ASCII.
  • An encoding sniffed by the chardet library, if you have it installed.
  • UTF-8
  • Windows-1252
+7  A: 

Use the Universal Encoding Detector:

>>> import chardet
>>> chardet.detect(urlread("http://google.cn/"))
{'encoding': 'GB2312', 'confidence': 0.99}

The other option would be to just use wget:

  import os
  h = os.popen('wget -q -O foo1.txt http://foo.html')
  h.close()
  s = open('foo1.txt').read()
rajax
This is no good as it fails sometimes. Also see: http://chardet.feedparser.org/docs/faq.html#faq.yippie (Yippie!)
Kalmi
The main problem with this approach that you ignore the page's explicitly specified character encoding.
Kalmi
Ok, then there isn't a silver bullet here I'm afraid - so write it yourself. :)
rajax
For example chardet detects origo.hu as ISO-8859-8 while that page is actually ISO-8859-2 as defined by a meta tag. That would mess things up badly.
Kalmi
Well... and how is foo1.txt encoded? :) The same way the webpage was encoded and we still don't what the encoding is.
Kalmi
And you've lost data, since the very first place to look is the Content-Type HTTP header, which was thrown away.
Glenn Maynard
@Kalmi: You link to the chardet faq; less than 10 lines down, he links to feedparser, which does what you want: http://code.google.com/p/feedparser/source/browse/trunk/feedparser/feedparser.py#3133(Granted, he only handles xml files, but 90% of the machinery you need is in there...)
Stobor
@Kalmi - There simply doesn't exist a solution that works every time, since many byte sequences can appear in many encodings.
Jonathan Feinberg
Stobor, Your answer(um... comment...) is the best so far. :)
Kalmi
+16  A: 

When you download a file with urllib or urllib2, you can find out whether a charset header was transmitted:

fp = urllib2.urlopen(request)
charset = fp.headers.getparam('charset')

You can use BeautifulSoup to locate a meta element in the HTML:

soup = BeatifulSoup.BeautifulSoup(data)
meta = soup.findAll('meta', {'http-equiv':lambda v:v.lower()=='content-type'})

If neither is available, browsers typically fall back to user configuration, combined with auto-detection. As rajax proposes, you could use the chardet module. If you have user configuration available telling you that the page should be Chinese (say), you may be able to do better.

Martin v. Löwis
I think it is `getparam`
kaizer.se
@kaizer.se: right; it's `get_param` in 3.x (but then, it's also urllib.request)
Martin v. Löwis
`BeatifulSoup` was deprecated in the 50s ;)
Otto Allmendinger
+1  A: 

instead of trying to get a page then figuring out the charset the browser would use, why not just use a browser to fetch the page and check what charset it uses..

from win32com.client import DispatchWithEvents
import threading


stopEvent=threading.Event()

class EventHandler(object):
    def OnDownloadBegin(self):
        pass

def waitUntilReady(ie):
    """
    copypasted from
    http://mail.python.org/pipermail/python-win32/2004-June/002040.html
    """
    if ie.ReadyState!=4:
        while 1:
            print "waiting"
            pythoncom.PumpWaitingMessages()
            stopEvent.wait(.2)
            if stopEvent.isSet() or ie.ReadyState==4:
                stopEvent.clear()
                break;

ie = DispatchWithEvents("InternetExplorer.Application", EventHandler)
ie.Visible = 0
ie.Navigate('http://kskky.info')
waitUntilReady(ie)
d = ie.Document
print d.CharSet
Ravi
just tested this on origo.hu and it works, albeit incredibly slowly - maybe try with the firefox activex component instead
Ravi
+1  A: 

It seems like you need a hybrid of the answers presented:

  1. Fetch the page using urllib
  2. Find <meta> tags using beautiful soup or other method
  3. If no meta tags exist, check the headers returned by urllib
  4. If that still doesn't give you an answer, use the universal encoding detector.

I honestly don't believe you're going to find anything better than that.

In fact if you read further into the FAQ you linked to in the comments on the other answer, that's what the author of detector library advocates.

If you believe the FAQ, this is what the browsers do (as requested in your original question) as the detector is a port of the firefox sniffing code.

Gareth Simpson
What I find odd is that there is no existing library/snippet for this.
Kalmi
Stobor pointed out the existence of feedparser.py (which is unfortunately only for XML), but contains most of the things I need.
Kalmi