【问题标题】:python scrapy how to code the parameter instead of using cmd: use Custom code in Scrapypython scrapy如何编码参数而不是使用cmd:在Scrapy中使用自定义代码
【发布时间】:2014-04-03 15:05:06
【问题描述】:

我正在使用 scrapy 0.20 和 puthon 2.7

我以前在cmd中这样做

 -s JOBDIR=crawls/somespider-1

处理重复的项目。 请注意,我已经对设置进行了更改

我不想在 cmd 中使用它。

有没有这样我可以在我的蜘蛛中输入代码?

谢谢

【问题讨论】:

  • 编辑主题,使新主题更相关..

标签: python python-2.7 scrapy


【解决方案1】:

这很容易。在 pipelines.py 中使用 dropitem 来删除项目。您可以使用自定义命令对程序内部的参数进行编码。

Here is example of custom code in scrapy

使用自定义命令(比如:scrapy crawl mycommand

你可以运行-s JOBDIR=crawls/somespider-1

例子:

创建一个目录commands,其中有scrapy.cfg 文件 在目录中创建一个文件mycommand.py

from scrapy.command import ScrapyCommand
from scrapy.cmdline import execute



class Command(ScrapyCommand):
    requires_project = True

    def short_desc(self):
        return "This is your custom command"


    def run(self, args, opts):
        args.append('scrapy')
        args.append('crawl')
        args.append('spider')##add what ever your syntax needs.In my case i want to get "scrapy crawl spider" in cmd
        execute(args)#send a list as parameter with command as a single element of it

现在转到 cmd 行并输入 scrapy mycommand。然后你的魔法就准备好了:-)

【讨论】:

  • 你能澄清更多
  • 我会给你一个样本
  • 我会向你解释。试着弄清楚它是如何工作的。如果你有问题,你可以再问
  • 我知道我可以做到self.jobdir = 'something,对吧?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-15
  • 2019-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多