Skip to content

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

. Added strip() to link texts in LinkExtractor in order to avoid extracting line breaks and such . Added test for restrict_xpaths in RegexLinkExtractor

elpolilla4a9a48a62951
diff --git a/scrapy/trunk/scrapy/link/__init__.py b/scrapy/trunk/scrapy/link/__init__.py
index c2dea185d..2d854075e 100644
--- a/scrapy/trunk/scrapy/link/__init__.py
+++ b/scrapy/trunk/scrapy/link/__init__.py
@@ -82,7 +82,7 @@ class LinkExtractor(FixedSGMLParser):
def handle_data(self, data):
if self.current_link and not self.current_link.text:
- self.current_link.text = data
+ self.current_link.text = data.strip()
def matches(self, url):
"""This extractor matches with any url, since
diff --git a/scrapy/trunk/scrapy/tests/sample_data/link_extractor/regex_linkextractor.html b/scrapy/trunk/scrapy/tests/sample_data/link_extractor/regex_linkextractor.html
index e0262fefc..602ea04cd 100644
--- a/scrapy/trunk/scrapy/tests/sample_data/link_extractor/regex_linkextractor.html
+++ b/scrapy/trunk/scrapy/tests/sample_data/link_extractor/regex_linkextractor.html
@@ -6,8 +6,8 @@
<body>
<div id='wrapper'>
<div id='subwrapper'>
-<area href='sample1.html'>sample 1</area>
-<a href='sample2.html'><img src='sample2.jpg'/></a>
+<area href='sample1.html' />
+<a href='sample2.html'>sample 2<img src='sample2.jpg'/></a>
</div>
<a href='sample3.html' title='sample 3'>sample 3 text</a>
<a href='sample3.html'>sample 3 repetition</a>
diff --git a/scrapy/trunk/scrapy/tests/test_link.py b/scrapy/trunk/scrapy/tests/test_link.py
index 9adc074a5..535b9710f 100644
--- a/scrapy/trunk/scrapy/tests/test_link.py
+++ b/scrapy/trunk/scrapy/tests/test_link.py
@@ -79,36 +79,41 @@ class RegexLinkExtractorTestCase(unittest.TestCase):
lx = RegexLinkExtractor()
self.assertEqual([link for link in lx.extract_links(self.response)],
- [ Link(url='http://example.com/sample1.html', text='sample 1'),
- Link(url='http://example.com/sample2.html', text=''),
- Link(url='http://example.com/sample3.html', text='sample 3 text'),
- Link(url='http://www.google.com/something', text='') ])
+ [ Link(url='http://example.com/sample1.html', text=u''),
+ Link(url='http://example.com/sample2.html', text=u'sample 2'),
+ Link(url='http://example.com/sample3.html', text=u'sample 3 text'),
+ Link(url='http://www.google.com/something', text=u'') ])
lx = RegexLinkExtractor(allow=('sample', ))
self.assertEqual([link for link in lx.extract_links(self.response)],
- [ Link(url='http://example.com/sample1.html', text='sample 1'),
- Link(url='http://example.com/sample2.html', text=''),
- Link(url='http://example.com/sample3.html', text='sample 3 text') ])
+ [ Link(url='http://example.com/sample1.html', text=u''),
+ Link(url='http://example.com/sample2.html', text=u'sample 2'),
+ Link(url='http://example.com/sample3.html', text=u'sample 3 text') ])
lx = RegexLinkExtractor(allow=('sample', ), unique=False)
self.assertEqual([link for link in lx.extract_links(self.response)],
- [ Link(url='http://example.com/sample1.html', text='sample 1'),
- Link(url='http://example.com/sample2.html', text=''),
- Link(url='http://example.com/sample3.html', text='sample 3 text'),
- Link(url='http://example.com/sample3.html', text='sample 3 repetition') ])
+ [ Link(url='http://example.com/sample1.html', text=u''),
+ Link(url='http://example.com/sample2.html', text=u'sample 2'),
+ Link(url='http://example.com/sample3.html', text=u'sample 3 text'),
+ Link(url='http://example.com/sample3.html', text=u'sample 3 repetition') ])
lx = RegexLinkExtractor(allow=('sample', ), deny=('3', ))
self.assertEqual([link for link in lx.extract_links(self.response)],
- [ Link(url='http://example.com/sample1.html', text='sample 1'),
- Link(url='http://example.com/sample2.html', text='') ])
+ [ Link(url='http://example.com/sample1.html', text=u''),
+ Link(url='http://example.com/sample2.html', text=u'sample 2') ])
lx = RegexLinkExtractor(allow_domains=('google.com', ))
self.assertEqual([link for link in lx.extract_links(self.response)],
- [ Link(url='http://www.google.com/something', text='') ])
+ [ Link(url='http://www.google.com/something', text=u'') ])
lx = RegexLinkExtractor(tags=('img', ), attrs=('src', ))
self.assertEqual([link for link in lx.extract_links(self.response)],
- [ Link(url='http://example.com/sample2.jpg', text='') ])
+ [ Link(url='http://example.com/sample2.jpg', text=u'') ])
+
+ lx = RegexLinkExtractor(restrict_xpaths=('//div[@id="subwrapper"]', ))
+ self.assertEqual([link for link in lx.extract_links(self.response)],
+ [ Link(url='http://example.com/sample1.html', text=u''),
+ Link(url='http://example.com/sample2.html', text=u'sample 2') ])
def test_matches(self):
url1 = 'http://lotsofstuff.com/stuff1/index'