python scrapy如何编码参数而不是使用cmd:在Scrapy中使用自定义代码 [英] python scrapy how to code the parameter instead of using cmd: use Custom code in Scrapy

查看:45
本文介绍了python scrapy如何编码参数而不是使用cmd:在Scrapy中使用自定义代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是带有 puthon 2.7 的scrapy 0.20

I am using scrapy 0.20 with puthon 2.7

我曾经在 cmd 中这样做过

i used to do this in cmd

 -s JOBDIR=crawls/somespider-1

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

我不想在 cmd 中使用它.

I dont' want to use that in cmd.

无论如何我可以在我的蜘蛛中输入代码吗?

谢谢

推荐答案

就是这么简单.使用 pipelines.py 中的 dropitem 删除项目.您可以使用自定义命令对程序内部的参数进行编码.

It's so easy. Use dropitem in pipelines.py to drop the item. And you can use custom command to code the parameter inside of program.

这里是scrapy中的自定义代码示例

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

Using the custom command (say : scrapy crawl mycommand)

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

示例:

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

Create a directory commands where you have scrapy.cfg file Inside the directory create a file 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.那么你的魔法就准备好了:-)

Now go to cmd line and type scrapy mycommand. Then your magic is ready :-)

这篇关于python scrapy如何编码参数而不是使用cmd:在Scrapy中使用自定义代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆