How to download any(!) webpage with correct charset in python? How to download any(!) webpage with correct charset in python? python python

How to download any(!) webpage with correct charset in python?


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.


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()


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.