I'm trying to rewrite this piece of code to use ItemLoader
class:
import scrapy
from ..items import Book
class BasicSpider(scrapy.Spider):
...
def parse(self, response):
item = Book()
# notice I only grab the first book among many there are on the page
item['title'] = response.xpath('//*[@class="link linkWithHash detailsLink"]/@title')[0].extract()
return item
The above works perfectly well. And now the same with ItemLoader
:
from scrapy.loader import ItemLoader
class BasicSpider(scrapy.Spider):
...
def parse(self, response):
l = ItemLoader(item=Book(), response=response)
l.add_xpath('title', '//*[@class="link linkWithHash detailsLink"]/@title'[0]) # this does not work - returns an empty dict
# l.add_xpath('title', '//*[@class="link linkWithHash detailsLink"]/@title') # this of course work but returns every book title there is on page, not just the first one which is required
return l.load_item()
So I only want to grab the first book title, how do I achieve that?