Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9233379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:32:23+00:00 2026-06-18T06:32:23+00:00

Looking for an easy way to get the charset/encoding information of an HTTP response

  • 0

Looking for an easy way to get the charset/encoding information of an HTTP response using Python urllib2, or any other Python library.

>>> url = 'http://some.url.value'
>>> request = urllib2.Request(url)
>>> conn = urllib2.urlopen(request)
>>> response_encoding = ?

I know that it is sometimes present in the ‘Content-Type’ header, but that header has other information, and it’s embedded in a string that I would need to parse. For example, the Content-Type header returned by Google is

>>> conn.headers.getheader('content-type')
'text/html; charset=utf-8'

I could work with that, but I’m not sure how consistent the format will be. I’m pretty sure it’s possible for charset to be missing entirely, so I’d have to handle that edge case. Some kind of string split operation to get the ‘utf-8’ out of it seems like it has to be the wrong way to do this kind of thing.

>>> content_type_header = conn.headers.getheader('content-type')
>>> if '=' in content_type_header:
>>>  charset = content_type_header.split('=')[1]

That’s the kind of code that feels like it’s doing too much work. I’m also not sure if it will work in every case. Does anyone have a better way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:32:24+00:00Added an answer on June 18, 2026 at 6:32 am

    To parse http header you could use cgi.parse_header():

    _, params = cgi.parse_header('text/html; charset=utf-8')
    print params['charset'] # -> utf-8
    

    Or using the response object:

    response = urllib2.urlopen('http://example.com')
    response_encoding = response.headers.getparam('charset')
    # or in Python 3: response.headers.get_content_charset(default)
    

    In general the server may lie about the encoding or do not report it at all (the default depends on content-type) or the encoding might be specified inside the response body e.g., <meta> element in html documents or in xml declaration for xml documents. As a last resort the encoding could be guessed from the content itself.

    You could use requests to get Unicode text:

    import requests # pip install requests
    
    r = requests.get(url)
    unicode_str = r.text # may use `chardet` to auto-detect encoding
    

    Or BeautifulSoup to parse html (and convert to Unicode as a side-effect):

    from bs4 import BeautifulSoup # pip install beautifulsoup4
    
    soup = BeautifulSoup(urllib2.urlopen(url)) # may use `cchardet` for speed
    # ...
    

    Or bs4.UnicodeDammit directly for arbitrary content (not necessarily an html):

    from bs4 import UnicodeDammit
    
    dammit = UnicodeDammit(b"Sacr\xc3\xa9 bleu!")
    print(dammit.unicode_markup)
    # -> Sacré bleu!
    print(dammit.original_encoding)
    # -> utf-8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, is there any easy way to get information when user will change time
I'm looking for an easy way to get width and height dimensions for image
Is there an easy way to examine the HTTP GET/POST/OPTIONS/HEAD etc requests being made
I am looking for an easy way to get files that are situated on
I am looking for an easy way to be able to get a value
I'm looking for a built-in (or easy-to-implement) way to get Core Data undo/redo action
Possible Duplicate: Frequency Analyzer in C# I am looking for easy way(or any way)
I am looking for an easy way to get objects into MS Excel. (I
Is there any easy/short way to get the worksheet object of the new sheet
I am looking for an easy way to get the SID for the current

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.