简体   繁体   English

从Spider Scrapy的“设置”中调用函数

[英]Call a function in Settings from spider Scrapy

I have two spiders A and B . 我有两个蜘蛛AB I need to call a function which is defined in the spider settings.py file 我需要调用在Spider settings.py文件中定义的函数

Project Name |--Project Name | |-- spiders | | |-- __init__.py | | |-- A.py | | |-- B.py | |-- __init__.py | |-- items.py | |-- pipelines.py | |-- settings.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中有一个功能,我需要在蜘蛛关闭时从A.py和B.py访问

settings.py settings.py

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

This is what I have tried from A.py: 这是我从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? 我做错了什么或其他任何方式吗?

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. 导入文件时,Python仅搜索当前目录,入口点脚本运行所在的目录以及sys.path,其中包括诸如软件包安装目录之类的位置。 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

What worked for me, thanks to the answer that @Jose posted, was that as settings.py was in a different directory than the spider which I was running and Python only searches the current directory. 感谢@Jose发布的答案,对我有用的是, settings.py与我正在运行的Spider不在同一个目录中,Python仅搜索当前目录。

So I tried checking the path of the file that it gives every-time I run the spider and apparently the path that I get was 因此,我尝试检查每次运行Spider时它提供的文件的路径,并且显然获得的路径是

/tmp/unpacked-eggs/__main__.egg/project name/spiders

So, what I had to do was: 所以,我要做的是:

import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + '/../')
import settings

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM