我正在开发我的第一个 Python 项目,并且正在使用 Scrapy.org 框架。我正在尝试使用 IF 语句来确定价格是否为空,因此它不会存储在我的 CSV 文件中。
出于某种原因,我收到了缩进异常。
IndentationError:需要一个缩进块
IF 语句位于此代码段的末尾。
感谢大家花时间帮助我!
代码:
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