Python Scrapy - 运行蜘蛛

时间:2016-09-01 14:41:54

标签: python-2.7 scrapy scrapy-spider

在Windows机器上运行Python27 ...尝试使用Scrapy

遵循基本的Scrapy教程@ http://doc.scrapy.org/en/latest/intro/overview.html

我创建了以下蜘蛛并将其保存为Test2 @ C:\ Python27 \ Scrapy

import scrapy


class StackOverflowSpider(scrapy.Spider):
name = 'stackoverflow'
start_urls = ['http://stackoverflow.com/questions?sort=votes']

def parse(self, response):
    for href in response.css('.question-summary h3 a::attr(href)'):
        full_url = response.urljoin(href.extract())
        yield scrapy.Request(full_url, callback=self.parse_question)

def parse_question(self, response):
    yield {
        'title': response.css('h1 a::text').extract_first(),
        'votes': response.css('.question .vote-count-post::text').extract_first(),
        'body': response.css('.question .post-text').extract_first(),
        'tags': response.css('.question .post-tag::text').extract(),
        'link': response.url,
    }

下一步告诉我使用蜘蛛 scrapy runspider stackoverflow_spider.py -o top-stackoverflow-questions.json

但我不知道在哪里运行那行代码。

我习惯在我的python文件末尾运行打印或存储到csv命令以检索结果。

当然这是一个很容易解决的问题,但我没有得到它..提前谢谢。

1 个答案:

答案 0 :(得分:1)

您需要在您使用的任何命令行实用程序中执行runspider命令,例如: Cygwin,cmd等。

该命令将在运行命令的目录中创建一个名为top-stackoverflow-questions.json的文件。