mirror of https://github.com/scrapy/scrapy.git
remove_entities: added support for common browser hack for numeric character references in the 80-9F range
This commit is contained in:
parent
c3f7bfea1f
commit
f2c2609e42
|
|
@ -30,6 +30,8 @@ class UtilsMarkupTest(unittest.TestCase):
|
|||
u'a < b c six')
|
||||
self.assertEqual(remove_entities('x≤y'), u'x\u2264y')
|
||||
|
||||
# check browser hack for numeric character references in the 80-9F range
|
||||
self.assertEqual(remove_entities('x™y', encoding='cp1252'), u'x\u2122y')
|
||||
|
||||
def test_replace_tags(self):
|
||||
# make sure it always return uncode
|
||||
|
|
|
|||
|
|
@ -37,6 +37,12 @@ def remove_entities(text, keep=(), remove_illegal=True, encoding='utf-8'):
|
|||
number = int(entity_body, 16)
|
||||
else:
|
||||
number = int(entity_body, 10)
|
||||
# Numeric character references in the 80-9F range are typically
|
||||
# interpreted by browsers as representing the characters mapped
|
||||
# to bytes 80-9F in the Windows-1252 encoding. For more info
|
||||
# see: http://en.wikipedia.org/wiki/Character_encodings_in_HTML
|
||||
if 0x80 <= number <= 0x9f:
|
||||
return chr(number).decode('cp1252')
|
||||
except ValueError:
|
||||
number = None
|
||||
else:
|
||||
|
|
|
|||
Loading…
Reference in New Issue