我有两个蜘蛛A
和B
。 我需要调用它在蜘蛛settings.py文件中定义的函数从蜘蛛Scrapy调用一个函数设置
Project Name |--Project Name | |-- spiders | | |-- __init__.py | | |-- A.py | | |-- B.py | |-- __init__.py | |-- items.py | |-- pipelines.py | |-- settings.py
有settings.py中的函数,这是我需要从A.py和B.py来访问近蜘蛛
settings.py
def revoke_ip():
logging.info('Revoking access')
。这是我从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()
但是这件事情不工作,也不是什么提到here
有什么,我做错了或任何其他方式做到这一点?
而我该如何调用撤销函数settings.revoke()? 如果我从'scrapy crawl A'的外部'project name'文件夹中运行爬虫,我得到一个错误'ImportError:No module named settings' – Vaulstein
这个东西部分地解决了我的问题,所以我打开了答案。谢谢! – Vaulstein
如果您从其他文件夹运行项目,则必须指定包含要使用的模块的文件夹。您可以尝试使用设置模块的绝对路径。不客气,我希望它有帮助。 –