Scrapy 返回零结果

Scrapy returning zero results

我正在尝试学习如何使用 scrapy,并尝试做一个我认为很简单的项目。我正试图从一个网页中提取 2 条数据——不需要抓取额外的链接。但是,我的代码似乎 returning 为零结果。我已经在 Scrapy Shell 中测试了 xpaths,并且都 return 了预期的结果。

我的item.py是:

import scrapy

class StockItem(scrapy.Item):
    quote = scrapy.Field()
    time = scrapy.Field()

我的蜘蛛,名为 stockscrapy.py,是:

import scrapy

class StockSpider(scrapy.Spider):
    name = "ugaz"
    allowed_domains = ["nasdaq.com"]
    start_urls = ["http://www.nasdaq.com/symbol/ugaz/"]

def parse(self, response):
    stock = StockItem()
    stock['quote'] = response.xpath('//*[@id="qwidget_lastsale"]/text()').extract()
    stock['time'] = response.xpath('//*[@id="qwidget_markettime"]/text()').extract()
    return stock

要运行脚本,我用命令行:

scrapy crawl ugaz -o stocks.csv

非常感谢任何帮助。

您需要缩进解析块。

import scrapy

class StockSpider(scrapy.Spider):
    name = "ugaz"
    allowed_domains = ["nasdaq.com"]
    start_urls = ["http://www.nasdaq.com/symbol/ugaz/"]

    # Indent this block
    def parse(self, response):
        stock = StockItem()
        stock['quote'] = response.xpath('//*[@id="qwidget_lastsale"]/text()').extract()
        stock['time'] = response.xpath('//*[@id="qwidget_markettime"]/text()').extract()
        return stock