Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requests.get() ignores charset=UTF-8 and BOM #654

Closed
jadient opened this issue Jun 6, 2012 · 6 comments
Closed

requests.get() ignores charset=UTF-8 and BOM #654

jadient opened this issue Jun 6, 2012 · 6 comments

Comments

@jadient
Copy link

jadient commented Jun 6, 2012

I've seen some discussion that charset=UTF-8 isn't sufficient because requests looks at the http headers rather than the body of the request (and I can understand the desire to avoid parsing the html looking for the META tag). However, if a page has a UTF-8 Byte Order Mark, I'd think that would be sufficient indication to treat the body as UTF-8.

@kennethreitz
Copy link
Contributor

If the server doesn't specify an encoding, one will automatically be detected via chardet

@jadient
Copy link
Author

jadient commented Jun 6, 2012

If you're saying that requests uses chardet, then this isn't working properly for me. Perhaps I need to dive into the code to see what's happening?

@kennethreitz
Copy link
Contributor

Requests uses chardet if it is available, yes.

Can you share the URL you're having trouble with?

@jadient
Copy link
Author

jadient commented Jun 6, 2012

@kennethreitz
Copy link
Contributor

Ah, this is because the server responds with 'content-type': 'text/html'.

This is standards compliance behavior: http://en.wikipedia.org/wiki/ISO/IEC_8859-1#History

You can do the following to ignore server-set encodings:

r = requests.get('http://www.stanford.edu/dept/visitorinfo/activities/dining.html')
r.encoding = None

Then, the contents of r.text will be accurate, as the chardet detection will kick in.

@jadient
Copy link
Author

jadient commented Jun 6, 2012

If there is a BOM, wouldn't it be reasonable to override text/html?

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants