-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
119 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from lxml import etree | ||
from lxml.etree import XMLParser as _UnsafeXMLParser | ||
from lxml.html import HTMLParser as _HTMLParser | ||
|
||
|
||
class _LXMLBaseParser(object): | ||
|
||
def __init__(self, parser_cls): | ||
self._parser = parser_cls(recover=True, encoding='utf8') | ||
|
||
def parse(self, text, base_url): | ||
body = text.strip().replace('\x00', '').encode('utf8') or b'<html/>' | ||
root = etree.fromstring(body, parser=self._parser, base_url=base_url) | ||
if root is None: | ||
root = etree.fromstring(b'<html/>', parser=self._parser, | ||
base_url=base_url) | ||
return root | ||
|
||
|
||
class HTMLParser(_LXMLBaseParser): | ||
|
||
def __init__(self): | ||
super(HTMLParser, self).__init__(_HTMLParser) | ||
|
||
|
||
class _XMLParser(_UnsafeXMLParser): | ||
|
||
def __init__(self, *args, **kwargs): | ||
kwargs.setdefault('resolve_entities', False) | ||
super(_XMLParser, self).__init__(*args, **kwargs) | ||
|
||
|
||
class XMLParser(_LXMLBaseParser): | ||
|
||
def __init__(self): | ||
super(XMLParser, self).__init__(_XMLParser) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from parsel.parser import HTMLParser | ||
|
||
HTML_PARSER = HTMLParser() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from parsel.parser import XMLParser | ||
|
||
XML_PARSER = XMLParser() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# -*- coding:utf-8 -*- | ||
|
||
|
||
from unittest import TestCase | ||
from warnings import catch_warnings | ||
|
||
from parsel.selector import create_root_node, SafeXMLParser | ||
from lxml.html import HTMLParser | ||
|
||
|
||
class TestDeprecations(TestCase): | ||
|
||
def test_create_root_node(self): | ||
with catch_warnings(record=True) as warnings: | ||
create_root_node(u'…', HTMLParser) | ||
self.assertEqual(len(warnings), 1) | ||
|
||
def test_SafeXMLParser(self): | ||
with catch_warnings(record=True) as warnings: | ||
parser = SafeXMLParser() | ||
self.assertEqual(len(warnings), 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters