mirror of https://github.com/scrapy/scrapy.git
Merge pull request #1565 from starrify/fix-1564
[MRG+1] fixed: Issue #1564 (Incorrectly picked URL in `scrapy.linkextractors.regex.RegexLinkExtractor` when there is a `<base>` tag. )
This commit is contained in:
commit
17aba44f16
|
|
@ -1,7 +1,7 @@
|
|||
import re
|
||||
from six.moves.urllib.parse import urljoin
|
||||
|
||||
from w3lib.html import remove_tags, replace_entities, replace_escape_chars
|
||||
from w3lib.html import remove_tags, replace_entities, replace_escape_chars, get_base_url
|
||||
|
||||
from scrapy.link import Link
|
||||
from .sgml import SgmlLinkExtractor
|
||||
|
|
@ -31,7 +31,7 @@ class RegexLinkExtractor(SgmlLinkExtractor):
|
|||
return clean_url
|
||||
|
||||
if base_url is None:
|
||||
base_url = urljoin(response_url, self.base_url) if self.base_url else response_url
|
||||
base_url = get_base_url(response_text, response_url, response_encoding)
|
||||
|
||||
links_text = linkre.findall(response_text)
|
||||
return [Link(clean_url(url).encode(response_encoding),
|
||||
|
|
|
|||
|
|
@ -124,9 +124,6 @@ class SgmlLinkExtractor(FilteringLinkExtractor):
|
|||
restrict_xpaths=restrict_xpaths, restrict_css=restrict_css,
|
||||
canonicalize=canonicalize, deny_extensions=deny_extensions)
|
||||
|
||||
# FIXME: was added to fix a RegexLinkExtractor testcase
|
||||
self.base_url = None
|
||||
|
||||
def extract_links(self, response):
|
||||
base_url = None
|
||||
if self.restrict_xpaths:
|
||||
|
|
|
|||
|
|
@ -190,3 +190,20 @@ class RegexLinkExtractorTestCase(unittest.TestCase):
|
|||
Link(url='http://example.org/item1.html', text=u'Item 1', nofollow=False),
|
||||
Link(url='http://example.org/item3.html', text=u'Item 3', nofollow=False),
|
||||
])
|
||||
|
||||
def test_html_base_href(self):
|
||||
html = """
|
||||
<html>
|
||||
<head>
|
||||
<base href="http://b.com/">
|
||||
</head>
|
||||
<body>
|
||||
<a href="test.html"></a>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
response = HtmlResponse("http://a.com/", body=html)
|
||||
lx = RegexLinkExtractor()
|
||||
self.assertEqual([link for link in lx.extract_links(response)], [
|
||||
Link(url='http://b.com/test.html', text=u'', nofollow=False),
|
||||
])
|
||||
|
|
|
|||
Loading…
Reference in New Issue