Scrapy, a fast high-level web crawling & scraping framework for Python.
replace unencodeable codepoints with html entities. fixes #562 and #285
Daniel Graña66829c962f75
diff --git a/scrapy/contrib/linkextractors/sgml.py b/scrapy/contrib/linkextractors/sgml.pyindex d8f6ae4ec..9a63fcd53 100644--- a/scrapy/contrib/linkextractors/sgml.py+++ b/scrapy/contrib/linkextractors/sgml.py@@ -121,7 +121,7 @@ class SgmlLinkExtractor(BaseSgmlLinkExtractor):body = u''.join(ffor x in self.restrict_xpathsfor f in sel.xpath(x).extract()- ).encode(response.encoding)+ ).encode(response.encoding, errors='xmlcharrefreplace')else:body = response.bodydiff --git a/scrapy/tests/test_contrib_linkextractors.py b/scrapy/tests/test_contrib_linkextractors.pyindex 4135b086c..19d122a89 100644--- a/scrapy/tests/test_contrib_linkextractors.py+++ b/scrapy/tests/test_contrib_linkextractors.py@@ -236,6 +236,13 @@ class SgmlLinkExtractorTestCase(unittest.TestCase):self.assertEqual(lx.extract_links(response),[Link(url='http://example.org/about.html', text=u'About us\xa3')])+ def test_restrict_xpaths_with_html_entities(self):+ html = '<html><body><p><a href="/♥/you?c=€">text</a></p></body></html>'+ response = HtmlResponse("http://example.org/somepage/index.html", body=html, encoding='iso8859-15')+ links = SgmlLinkExtractor(restrict_xpaths='//p').extract_links(response)+ self.assertEqual(links,+ [Link(url='http://example.org/%E2%99%A5/you?c=%E2%82%AC', text=u'text')])+def test_restrict_xpaths_concat_in_handle_data(self):"""html entities cause SGMLParser to call handle_data hook twice"""body = """<html><body><div><a href="/foo">>\xbe\xa9<\xb6\xab</a></body></html>"""