从蜘蛛 Scrapy 调用设置中的函数 [英] Call a function in Settings from spider Scrapy

查看:22
本文介绍了从蜘蛛 Scrapy 调用设置中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个蜘蛛AB.我需要调用一个在spider settings.py 文件中定义的函数

I have two spiders A and B. I need to call a function which is defined in the spider settings.py file

项目名称|--项目名称||-- 蜘蛛|||-- __init__.py|||-- A.py|||-- B.py||-- __init__.py||-- items.py||-- 管道.py||-- settings.py

settings.py 中有一个函数,我需要在蜘蛛关闭时从 A.py 和 B.py 访问该函数

There is a function in settings.py, which I need to access from A.py and B.py on close of spider

settings.py

settings.py

def revoke_ip():
    logging.info('Revoking access')

这是我从 A.py 中尝试过的:

This is what I have tried from A.py:

def closed(self, reason):
    logging.info('Spider terminating because of %s' % reason)
    current_project_settings = get_project_settings()
    revoke_ip_call = getattr(current_project_settings, "revoke_ip")
    revoke_ip_call()

但是这件事不起作用,上面提到的也不起作用 这里

But this thing doesn't work nor does what is mentioned here

我做错了什么或有其他方法吗?

Is there anything that I am doing wrong or any other way to do it?

推荐答案

在导入文件时,Python 仅搜索当前目录、运行入口点脚本的目录以及 sys.path 包括的位置,例如包安装目录.您可以导入设置文件来调用该函数.为此,请将其添加到您的函数中:

When importing a file, Python only searches the current directory, the directory that the entry-point script is running from, and sys.path which includes locations such as the package installation directory. You can import the settings file to call the function. To do this, add this to your function:

import sys
sys.path.insert(0, '../')
import settings

这篇关于从蜘蛛 Scrapy 调用设置中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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