mirror of https://github.com/scrapy/scrapy.git
Fix #199 encoding error concatenating link text
This commit is contained in:
parent
8e77f27897
commit
cc69b3aa4c
|
|
@ -115,13 +115,15 @@ class SgmlLinkExtractor(BaseSgmlLinkExtractor):
|
|||
base_url = None
|
||||
if self.restrict_xpaths:
|
||||
hxs = HtmlXPathSelector(response)
|
||||
html = ''.join(''.join(html_fragm for html_fragm in hxs.select(xpath_expr).extract()) \
|
||||
for xpath_expr in self.restrict_xpaths)
|
||||
base_url = get_base_url(response)
|
||||
body = u''.join(f
|
||||
for x in self.restrict_xpaths
|
||||
for f in hxs.select(x).extract()
|
||||
).encode(response.encoding)
|
||||
else:
|
||||
html = response.body
|
||||
body = response.body
|
||||
|
||||
links = self._extract_links(html, response.url, response.encoding, base_url)
|
||||
links = self._extract_links(body, response.url, response.encoding, base_url)
|
||||
links = self._process_links(links)
|
||||
return links
|
||||
|
||||
|
|
|
|||
|
|
@ -217,6 +217,15 @@ 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_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>"""
|
||||
response = HtmlResponse("http://example.org", body=body, encoding='gb18030')
|
||||
lx = SgmlLinkExtractor(restrict_xpaths="//div")
|
||||
self.assertEqual(lx.extract_links(response),
|
||||
[Link(url='http://example.org/foo', text=u'>\u4eac<\u4e1c',
|
||||
fragment='', nofollow=False)])
|
||||
|
||||
def test_deny_extensions(self):
|
||||
html = """<a href="page.html">asd</a> and <a href="photo.jpg">"""
|
||||
response = HtmlResponse("http://example.org/", body=html)
|
||||
|
|
|
|||
Loading…
Reference in New Issue