Scrapy and xpath to crawl my site and export URLs - what am I doing wrong?
Question: I'm trying to set up a basic Scrapy to crawl my website and extract all the
page URLs of my site. I would think this would be fairly easy.

Here's my items.py, copied from the tutorial:

    
    
    from scrapy.item import Item, Field
    class Website(Item):
        name = Field()
        description = Field()
        url = Field()
    

Here's my Spider, named example.py from the tutorial.

    
    
    from scrapy.spiders import Spider
    from scrapy.selector import Selector
    
    from cspenn.items import Website
    
    
    class DmozSpider(Spider):
        name = "cspenn"
        allowed_domains = ["christopherspenn.com"]
        start_urls = ["http://www.christopherspenn.com/"]
    
        def parse(self, response):
    
            sel = Selector(response)
            sites = sel.xpath('//a')
            items = []
    
            for site in sites:
                item = Website()
                item['name'] = site.xpath('a/text()').extract()
                item['url'] = site.xpath('a/@href').extract()
                item['description'] = site.xpath('text()').re('-\s[^\n]*\\r')
                items.append(item)
    
            return items
    

What I get in return from the bot is:

    
    
    scrapy crawl cspenn
    2016-04-13 13:15:25 [scrapy] INFO: Scrapy 1.0.5 started (bot: cspenn)
    2016-04-13 13:15:25 [scrapy] INFO: Optional features available: ssl, http11, boto
    2016-04-13 13:15:25 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'cspenn.spiders', 'SPIDER_MODULES': ['cspenn.spiders'], 'BOT_NAME': 'cspenn'}
    2016-04-13 13:15:25 [scrapy] INFO: Enabled extensions: CloseSpider, TelnetConsole, LogStats, CoreStats, SpiderState
    2016-04-13 13:15:26 [boto] DEBUG: Retrieving credentials from metadata server.
    2016-04-13 13:15:27 [boto] ERROR: Caught exception reading instance data
    Traceback (most recent call last):
      File "/usr/lib/python2.7/dist-packages/boto/utils.py", line 210, in retry_url
        r = opener.open(req, timeout=timeout)
      File "/usr/lib/python2.7/urllib2.py", line 431, in open
        response = self._open(req, data)
      File "/usr/lib/python2.7/urllib2.py", line 449, in _open
        '_open', req)
      File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
        result = func(*args)
      File "/usr/lib/python2.7/urllib2.py", line 1227, in http_open
        return self.do_open(httplib.HTTPConnection, req)
      File "/usr/lib/python2.7/urllib2.py", line 1197, in do_open
        raise URLError(err)
    URLError: <urlopen error timed out>
    2016-04-13 13:15:27 [boto] ERROR: Unable to read instance data, giving up
    2016-04-13 13:15:27 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, CookiesMiddleware, ChunkedTransferMiddleware, DownloaderStats
    2016-04-13 13:15:27 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
    2016-04-13 13:15:27 [scrapy] INFO: Enabled item pipelines: 
    2016-04-13 13:15:27 [scrapy] INFO: Spider opened
    2016-04-13 13:15:27 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
    2016-04-13 13:15:27 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
    2016-04-13 13:15:27 [scrapy] DEBUG: Crawled (200) <GET http://www.christopherspenn.com/> (referer: None)
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] DEBUG: Scraped from <200 http://www.christopherspenn.com/>
    {'description': [], 'name': [], 'url': []}
    2016-04-13 13:15:27 [scrapy] INFO: Closing spider (finished)
    2016-04-13 13:15:27 [scrapy] INFO: Dumping Scrapy stats:
    {'downloader/request_bytes': 222,
     'downloader/request_count': 1,
     'downloader/request_method_count/GET': 1,
     'downloader/response_bytes': 14302,
     'downloader/response_count': 1,
     'downloader/response_status_count/200': 1,
     'finish_reason': 'finished',
     'finish_time': datetime.datetime(2016, 4, 13, 17, 15, 27, 262789),
     'item_scraped_count': 93,
     'log_count/DEBUG': 96,
     'log_count/ERROR': 2,
     'log_count/INFO': 7,
     'response_received_count': 1,
     'scheduler/dequeued': 1,
     'scheduler/dequeued/memory': 1,
     'scheduler/enqueued': 1,
     'scheduler/enqueued/memory': 1,
     'start_time': datetime.datetime(2016, 4, 13, 17, 15, 27, 77084)}
    2016-04-13 13:15:27 [scrapy] INFO: Spider closed (finished)
    

What am I doing wrong? I followed the tutorial almost exactly. The desired
output is a CSV file of title, page URL, and description.


Answer: You are not making context-specific xpaths correctly. You already have the `a`
in the context, inside the `site` variable, no need to prepend `a` to the
XPath expressions inside the loop:

    
    
    sel = Selector(response)
    sites = sel.xpath('//a')
    for site in sites:
        item = Website()
        item['name'] = site.xpath('text()').extract()
        item['url'] = site.xpath('@href').extract()
        item['description'] = site.xpath('text()').re('-\s[^\n]*\\r')
        yield item
    

And, since you have the empty descriptions in the output as well, I suspect
the regular expression needs to be tweaked too. This though depends on what
exactly are you trying to extract from the link texts.

