<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import scrapy


class MyItem(scrapy.Item):
    item1 = scrapy.Field()
    item_url = scrapy.Field()
    item_address = scrapy.Field()
    item_list = scrapy.Field()


class MyLoader(scrapy.contrib.loader.ItemLoader):
    default_output_processor = TakeFirst()
    default_input_processor = MapCompose(unicode.strip)
    item_list_in = Identity()
    item_list_out = Identity()


class MySpider(scrapy.Spider):

    name = 'myspider'
    start_urls = ['some-url']

    def parse(self, response):
        uls = response.xpath('//div[@id=&quot;someID&quot;]//ul')
        for ul in uls:
            url = ul.xpath('.//li[1]/a/@href').extract()[0]
            loader = MyLoader(MyItem(), ul)
            loader.add_xpath('item1', './/li[1]/a/text()')
            loader.add_value('item_url', url)
            request = scrapy.Request(url, callback=self.parse_more)
            request.meta['item'] = loader.load_item()
            yield request

    def parse_more(self, response):
        loader = MyLoader(response.meta['item'], response=response)
        item_list = response.css('.nav-buttons.someclass').xpath('.//a/img/@alt').extract()
        loader.add_xpath('item_address', '//address//div[@class=&quot;address-info&quot;]/text()')
        loader.add_value('item_list', item_list)
        #import pdb; pdb.set_trace()
        if loader.get_output_value('item_address') == &quot;some address&quot;:
             loader.add_value('address_code', 'FR')
        yield loader.load_item()"><pre class="notranslate"><code class="notranslate">import scrapy


class MyItem(scrapy.Item):
    item1 = scrapy.Field()
    item_url = scrapy.Field()
    item_address = scrapy.Field()
    item_list = scrapy.Field()


class MyLoader(scrapy.contrib.loader.ItemLoader):
    default_output_processor = TakeFirst()
    default_input_processor = MapCompose(unicode.strip)
    item_list_in = Identity()
    item_list_out = Identity()


class MySpider(scrapy.Spider):

    name = 'myspider'
    start_urls = ['some-url']

    def parse(self, response):
        uls = response.xpath('//div[@id="someID"]//ul')
        for ul in uls:
            url = ul.xpath('.//li[1]/a/@href').extract()[0]
            loader = MyLoader(MyItem(), ul)
            loader.add_xpath('item1', './/li[1]/a/text()')
            loader.add_value('item_url', url)
            request = scrapy.Request(url, callback=self.parse_more)
            request.meta['item'] = loader.load_item()
            yield request

    def parse_more(self, response):
        loader = MyLoader(response.meta['item'], response=response)
        item_list = response.css('.nav-buttons.someclass').xpath('.//a/img/@alt').extract()
        loader.add_xpath('item_address', '//address//div[@class="address-info"]/text()')
        loader.add_value('item_list', item_list)
        #import pdb; pdb.set_trace()
        if loader.get_output_value('item_address') == "some address":
             loader.add_value('address_code', 'FR')
        yield loader.load_item()
</code></pre></div>
<p dir="auto">In pdb, for example, if we inspect a loader instance</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(pdb) loader.load_item()
{'item_address': 'someval', ....}
(pdb) type(loader.get_output_value('item_address'))
&lt;type: Nonetype&gt;"><pre class="notranslate"><code class="notranslate">(pdb) loader.load_item()
{'item_address': 'someval', ....}
(pdb) type(loader.get_output_value('item_address'))
&lt;type: Nonetype&gt;
</code></pre></div>
<p dir="auto">As a result, the if-block is never executing.</p>
<p dir="auto">Hi, following code snippet does not work as i expected:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="In [2]: from scrapy.contrib.loader import ItemLoader

In [3]: 

In [3]: from scrapy.item import Item, Field

In [4]: 

In [4]: class Product(Item):
   ...:         name = Field()
   ...:         comment = Field()
   ...:     

In [5]: p = Product({'name': 'xx1', 'comment': 'hi!'})

In [6]: p
Out[6]: {'comment': 'hi!', 'name': 'xx1'}

In [7]: l = ItemLoader(item=p)

In [8]: l.get_output_value('name')
Out[8]: []

In [9]: l.context
Out[9]: {'item': {'comment': 'hi!', 'name': 'xx1'}, 'response': None, 'selector': None}"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">2</span>]: <span class="pl-s1">from</span> <span class="pl-s1">scrapy</span>.<span class="pl-s1">contrib</span>.<span class="pl-s1">loader</span> <span class="pl-s1">import</span> <span class="pl-v">ItemLoader</span>

<span class="pl-v">In</span> [<span class="pl-c1">3</span>]: 

<span class="pl-v">In</span> [<span class="pl-c1">3</span>]: <span class="pl-k">from</span> <span class="pl-s1">scrapy</span>.<span class="pl-s1">item</span> <span class="pl-k">import</span> <span class="pl-v">Item</span>, <span class="pl-v">Field</span>

<span class="pl-v">In</span> [<span class="pl-c1">4</span>]: 

<span class="pl-v">In</span> [<span class="pl-c1">4</span>]: <span class="pl-k">class</span> <span class="pl-v">Product</span>(<span class="pl-v">Item</span>):
   ...:         <span class="pl-s1">name</span> <span class="pl-c1">=</span> <span class="pl-v">Field</span>()
   ...:         <span class="pl-s1">comment</span> <span class="pl-c1">=</span> <span class="pl-v">Field</span>()
   ...:     

<span class="pl-v">In</span> [<span class="pl-c1">5</span>]: <span class="pl-s1">p</span> <span class="pl-c1">=</span> <span class="pl-v">Product</span>({<span class="pl-s">'name'</span>: <span class="pl-s">'xx1'</span>, <span class="pl-s">'comment'</span>: <span class="pl-s">'hi!'</span>})

<span class="pl-v">In</span> [<span class="pl-c1">6</span>]: <span class="pl-s1">p</span>
<span class="pl-v">Out</span>[<span class="pl-c1">6</span>]: {<span class="pl-s">'comment'</span>: <span class="pl-s">'hi!'</span>, <span class="pl-s">'name'</span>: <span class="pl-s">'xx1'</span>}

<span class="pl-v">In</span> [<span class="pl-c1">7</span>]: <span class="pl-s1">l</span> <span class="pl-c1">=</span> <span class="pl-v">ItemLoader</span>(<span class="pl-s1">item</span><span class="pl-c1">=</span><span class="pl-s1">p</span>)

<span class="pl-v">In</span> [<span class="pl-c1">8</span>]: <span class="pl-s1">l</span>.<span class="pl-en">get_output_value</span>(<span class="pl-s">'name'</span>)
<span class="pl-v">Out</span>[<span class="pl-c1">8</span>]: []

<span class="pl-v">In</span> [<span class="pl-c1">9</span>]: <span class="pl-s1">l</span>.<span class="pl-s1">context</span>
<span class="pl-v">Out</span>[<span class="pl-c1">9</span>]: {<span class="pl-s">'item'</span>: {<span class="pl-s">'comment'</span>: <span class="pl-s">'hi!'</span>, <span class="pl-s">'name'</span>: <span class="pl-s">'xx1'</span>}, <span class="pl-s">'response'</span>: <span class="pl-c1">None</span>, <span class="pl-s">'selector'</span>: <span class="pl-c1">None</span>}</pre></div>
<p dir="auto">I would expect to see a result of 'xx1' from get_output_value() function intuitively. Am i right about my expectation here?</p>
<p dir="auto">Regarding the code at item loader (<a href="https://github.com/scrapy/scrapy/blob/master/scrapy/contrib/loader/__init__.py#L88">https://github.com/scrapy/scrapy/blob/master/scrapy/contrib/loader/__init__.py#L88</a>), is it correct to check the 'context' or 'item' of the loader if relevant field_name is not available in the loader?</p>