Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

set base_url in kwargs to be fully backward compatible

Elias Dornelese50610bd3a8c
diff --git a/scrapy/selector/unified.py b/scrapy/selector/unified.py
index a8f80c84d..25cf6c98d 100644
--- a/scrapy/selector/unified.py
+++ b/scrapy/selector/unified.py
@@ -46,6 +46,7 @@ class Selector(ParselSelector, object_ref):
if response is not None:
text = response.body_as_unicode()
+ kwargs.setdefault('base_url', response.url)
self.response = response
super(Selector, self).__init__(text=text, type=st, root=root, **kwargs)
diff --git a/tests/test_selector.py b/tests/test_selector.py
index 2d6d8c439..19b807a3f 100644
--- a/tests/test_selector.py
+++ b/tests/test_selector.py
@@ -33,6 +33,13 @@ class SelectorTestCase(unittest.TestCase):
self.assertEqual([x.extract() for x in sel.xpath("concat(//input[@name='a']/@value, //input[@name='b']/@value)")],
[u'12'])
+ def test_root_base_url(self):
+ body = b'<html><form action="/path"><input name="a" /></form></html>'
+ url = "http://example.com"
+ response = TextResponse(url=url, body=body, encoding='utf-8')
+ sel = Selector(response)
+ self.assertEqual(url, sel.root.base)
+
def test_deprecated_root_argument(self):
with warnings.catch_warnings(record=True) as w:
root = etree.fromstring(u'<html/>')