0

I've tried to read previous answers regarding the UnboundLocalError, however I can't find any that's similar to mine. I tried setting nonlocal as well as global without luck.

I'm getting an UnboundLocalError: local variable 'product_color' referenced before assignment while running this piece of code, however, if I only run the first for-loop, I get no errors. I don't get why I cant do the same without getting an UnboundLocalError.

def parse_product(self, response):
    product_category = response.meta.get('Category')
    product_title = response.meta.get('Product Title')
    product_price = response.meta.get('Product price')
    product_url = response.meta.get('Product URL')
    main_image_url = response.meta.get('Main image URL')

    product_description = response.xpath('//p[@class="product-detail-description-text"]/text()').extract()

    images = response.xpath('//div[@class="product-detail-thumbnails"]')

    for image in images:
        alt_image_url = image.xpath('ul/li/img/@src').extract()

    colors = response.xpath('//ul[@class="input clearfix"]')

    for color in colors:
        product_color = color.xpath('li/a/@title').extract()

    yield {'Product Category': product_category, 'Product Title': product_title, 'Product price': product_price, 'Product URL': product_url, 'Main image': main_image_url, "Alt images": alt_image_url, 'Description': product_description, 'Product color': product_color}

UPDATE: Full error message

    Traceback (most recent call last):
    File "/usr/local/lib/python2.7/site-packages/scrapy/utils/defer.py", line 102, in iter_errback
yield next(it)
    File "/usr/local/lib/python2.7/site-packages/scrapy/spidermiddlewares/offsite.py", line 29, in process_spider_output
for x in result:
    File "/usr/local/lib/python2.7/site-packages/scrapy/spidermiddlewares/referer.py", line 339, in <genexpr>
return (_set_referer(r) for r in result or ())
    File "/usr/local/lib/python2.7/site-packages/scrapy/spidermiddlewares/urllength.py", line 37, in <genexpr>
return (r for r in result or () if _filter(r))
    File "/usr/local/lib/python2.7/site-packages/scrapy/spidermiddlewares/depth.py", line 58, in <genexpr>
return (r for r in result or () if _filter(r))
    File "/Users/stefanjohansen/Projects/Python/hm/hm/spiders/products.py", line 46, in parse_product
yield {'Product Category': product_category, 'Product Title': product_title, 'Product price': product_price, 'Product URL': product_url, 'Main image': main_image_url, "Alt images": alt_image_url, 'Description': product_description, 'Product color': product_color}
    UnboundLocalError: local variable 'product_color' referenced before assignment
Sjohansen
  • 35
  • 6
  • https://stackoverflow.com/questions/9264763/unboundlocalerror-in-python These is some good explanation in the above links accepted answer! Hope it helps. – anugrah Jul 24 '17 at 21:25
  • @anugrah - I've been through the first 10 answers and haven't gotten closer to an answer :/ But thank you! – Sjohansen Jul 24 '17 at 21:26
  • 2
    Are you sure your code gets into the for loop? I mean is there any value in `colors`? – Lafexlos Jul 24 '17 at 21:27
  • 2
    I suspect some times `colors` is empty, so you never actually assign `product_color = color.xpath(...)` so then when you `return {..., 'Product color': product_color}` it throws the unbound-local error – juanpa.arrivillaga Jul 24 '17 at 21:29
  • 1
    Note also that your for-loop simply overwrites the pervious values, so `product_color` simply contains the last value from your loop. Not sure if that is what you *want*. – juanpa.arrivillaga Jul 24 '17 at 21:30
  • @Lafexlos I was thinking the same. Good suggestion. He should check for it and may be define it beforehand by null object. – anugrah Jul 24 '17 at 21:31
  • @Lafexlos - yes, I'm quite certain so. Just tried to change the xpath to be certain, and the error still occurs. – Sjohansen Jul 24 '17 at 21:34
  • 1
    @Sjohansen it would be *very helpful* if you posted the full error message and stack trace. That usually tells you *exactly* where your error is coming from, if not, it usually narrows it down considerably. Again, I am almost certain you are looping over an empty `colors`. Put a `print` statement inside that loop to check. OR try putting something like `product_color = None` at the top of the function, and if the error disappears, then there is your answer... – juanpa.arrivillaga Jul 24 '17 at 21:37
  • @juanpa.arrivillaga - question updated with full error – Sjohansen Jul 24 '17 at 21:41
  • 1
    yeah, so now I am basically certain the root of your issue is that you never actually enter that for-loop. i.e. what @Lafexlos has been saying... – juanpa.arrivillaga Jul 24 '17 at 21:42
  • 1
    @juanpa.arrivillaga - I tried to print the xpath, and seems it is empty. Found out that the xpath was valid, but didn't return the desired object. I changed the xpath with a third path and voila, it worked. Thank you! – Sjohansen Jul 24 '17 at 21:51

0 Answers0