Search code examples
pythonscrapyindentationconditional-statementscraigslist

Python Conditional (IF, ELSE) Indentation Exception


I am working on my first Python project and am using the Scrapy.org framework. I am attempting to use an IF statement to determine if price is empty so it doesn't store in my CSV file.

For some reason I am receiving a indentation exception.

IndentationError: expected an indented block

The IF statement is located at the end of this code snippet.

Thank you all for taking the time to help me out!

Code:

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from craigslist_sample.items import CraigslistSampleItem

class MySpider(BaseSpider):
name = "craig"
allowed_domains = ["craigslist.org"]
start_urls = [
'http://longisland.craigslist.org/search/sss?sort=date&query=raptor+660&srchType=T',
'http://newyork.craigslist.org/search/sss?zoomToPosting=&query=raptor+660&srchType=T&minAsk=&maxAsk=&sort=date',
'http://hudsonvalley.craigslist.org/search/sss?zoomToPosting=&query=raptor+660&srchType=T&minAsk=&maxAsk=&sort=date',
'http://newjersey.craigslist.org/search/sss?zoomToPosting=&query=raptor+660&srchType=T&minAsk=&maxAsk=&sort=date',
'http://hartford.craigslist.org/search/sss?zoomToPosting=&query=raptor+660&srchType=T&minAsk=&maxAsk=&sort=date'
]

def parse(self, response):
    hxs = HtmlXPathSelector(response)
    titles = hxs.select("//p")
    items = []
    for titles in titles:
        item = CraigslistSampleItem()
        #item["date"] = titles.select('span[@class="itemdate"]/text()').extract()
        item ["title"] = titles.select("a/text()").extract()
        item ["link"] = titles.select("a/@href").extract()
        item ["price"] = titles.select('span[@class="itempnr"]/span[@class="itempp"]/text()').extract()
        if not items ["price"]:
            #do nothing
        else:
            items.append(item)
    return items

Solution

  • if, else, for, def etc. must be followed by a code block. A comment doesn't count. This is what the pass statement is for:

    if not item["price"]:
        pass
    else:
        items.append(item)
    

    That said, why not just invert the condition?

    if item["price"]:
        items.append(item)