Scrapy spider for JSON response is giving me error
<pre><code>import json
import scrapy

class SpidyQuotesSpider(scrapy.Spider):

    name = 'hotelspider'
    start_urls = [
     'https://tr.hotels.com/search/listings.json?destination-id=1648683&amp;q-check-out=2016-10-22&amp;q-destination=Didim,+T%C3%BCrkiye&amp;q-room-0-adults=2&amp;pg=2&amp;q-rooms=1&amp;start-index=7&amp;q-check-in=2016-10-21&amp;resolved-location=CITY:1648683:UNKNOWN:UNKNOWN&amp;q-room-0-children=0&amp;pn=1'
               ]


    def parse(self, response):
        myresponse = json.loads(response.body)
        data = myresponse.get('data')
        body = data.get('body')
        searchresults = body.get('searchResults')

         for item in searchresults.get('results', []):
            yield {
                'text': item[0]['altText']
            }
</code></pre>

<p><a href="https://i.stack.imgur.com/QzKuk.png" rel="nofollow">this is the screenshot of the error</a></p>

<p>I always get error when I run this script. Can anybody help me where I am doing wrong ? </p>

<p>I can't seem to reproduce your error but upon copying your code, I got a key error which pertains to your yield statement. See the code below:</p>

<pre><code>import scrapy
import json


class SpidyQuotesSpider(scrapy.Spider):
    name = "hotelspider"
    allowed_domains = ["tr.hotels.com"]
    start_urls = (
        'https://tr.hotels.com/search/listings.json?destination-id=1648683&amp;q-check-out=2016-10-22&amp;q-destination=Didim,+T%C3%BCrkiye&amp;q-room-0-adults=2&amp;pg=2&amp;q-rooms=1&amp;start-index=7&amp;q-check-in=2016-10-21&amp;resolved-location=CITY:1648683:UNKNOWN:UNKNOWN&amp;q-room-0-children=0&amp;pn=1',
    )

    def parse(self, response):
        myresponse = json.loads(response.body)
        data = myresponse.get('data')
        body = data.get('body')
        searchresults = body.get('searchResults')

        for item in searchresults.get('results', []):
            yield {
                'text': item['altText']
            }
</code></pre>

<p>Make sure you are indenting using the same amount of spaces or just use TAB. Though the indentation shown in your code seems fine. Try pasting mine and see what comes up.</p>

python json python-2.7 scrapy scrapy-spider