Skip to content
Advertisement

How do I include part of my code into ‘yield’?

Thank you for your time!

Each products, sometimes have more than one model. I got the model ‘name’ and ‘price’ of the respective models within a single product via a for loop.

But, how do I ‘transfer’ these details to the ‘yield’ section along with other variables of that same product? Below is my attempt, but i am not getting it correct. How do I edit the code, so that, it could record more than one models (along with the price) within a same product, wherever applicable:

    for i in resp['item']['models']:
        if i['name'] is not None:
            model = i['name']
            model_pricing = i['price']


    yield{
        'product': resp.get('item').get('name'),
        'rating': resp.get('item').get('item_rating').get('rating_star'),
        'review numbers': resp.get('item').get('cmt_count'),
        'viewcount': resp.get('item').get('view_count'),
        'likes': resp.get('item').get('liked_count'),
        'model_pricing': model_pricing,
        'model': model,
        'location': resp.get('item').get('shop_location')
        }

Advertisement

Answer

You should keep your yield inside for loop and if statement. You can try as follows:

for i in resp['item']['models']:
    if i['name'] is not None:
        model = i['name']
        model_pricing = i['price']

        yield {
            'product': resp.get('item').get('name'),
            'rating': resp.get('item').get('item_rating').get('rating_star'),
            'review numbers': resp.get('item').get('cmt_count'),
            'viewcount': resp.get('item').get('view_count'),
            'likes': resp.get('item').get('liked_count'),
            'model_pricing': model_pricing,
            'model': model,
            'location': resp.get('item').get('shop_location')
            }
Advertisement