JSON响应的Scrapy spider给了我错误

时间:2016-10-19 22:28:04

标签: python json python-2.7 scrapy scrapy-spider

import json
import scrapy

class SpidyQuotesSpider(scrapy.Spider):

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

this is the screenshot of the error

运行此脚本时总是出错。任何人都可以帮助我在哪里做错了吗?

2 个答案:

答案 0 :(得分:0)

我似乎无法重现您的错误,但在复制代码时,我收到了与您的yield语句相关的关键错误。请参阅以下代码:

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&q-check-out=2016-10-22&q-destination=Didim,+T%C3%BCrkiye&q-room-0-adults=2&pg=2&q-rooms=1&start-index=7&q-check-in=2016-10-21&resolved-location=CITY:1648683:UNKNOWN:UNKNOWN&q-room-0-children=0&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']
            }

确保使用相同数量的空格缩进或仅使用TAB。虽然您的代码中显示的缩进似乎很好。尝试粘贴我的,看看会发生什么。

答案 1 :(得分:0)

您正在将蜘蛛代码中的空格和制表符混合在一起(我在#34;编辑"函数中复制了您的代码):

enter image description here

Quoting Wikipedia" Python使用空格分隔控制流块" Indentation至关重要,您需要坚持使用空格或制表符。 Mixing the 2会导致这些IndentationError

尝试这样做:

enter image description here