scrapy在pipelines中获取项目根目录

scrapy在pipelines中获取项目根目录

项目结构

方法一

python 复制代码
import os
 
class MyPipeline:
    def process_item(self, item, spider):
        # 获取当前目录
        current_dir = os.path.dirname(os.path.abspath(__file__))
        # 向上移动到项目根目录
        project_root = os.path.abspath(os.path.join(current_dir, os.pardir))
        # 现在你可以使用project_root来访问项目根目录的文件
        print("项目根目录:", project_root)
        return item

方法二

python 复制代码
import os
 
class MyPipeline:
    def process_item(self, item, spider):
        # 获取当前工作目录
        current_dir = os.path.dirname(os.path.abspath(__file__))
        # 向上移动到项目根目录
        project_root = os.path.dirname(current_dir)
        # 现在你可以使用project_root来访问项目根目录的文件
        print("项目根目录:", project_root)
        return item

方法三

python 复制代码
import os
 
class MyPipeline:
    def process_item(self, item, spider):
        # 获取项目目录
        project_dir = os.getcwd()
        # 现在你可以使用project_root来访问项目根目录的文件
        print("项目根目录:", project_root)
        return item
相关推荐
小龙报1 天前
【Coze-AI智能体平台】低代码省时高效:Coze 应用开发全流程指南
java·人工智能·python·深度学习·低代码·chatgpt·交互
技术钱1 天前
LCEL表达式与Runnable可运行协议
python
Echo_NGC22371 天前
【论文解读】Attention Is All You Need —— AI 时代的“开山之作“,经典中的经典(transformer小白导读)
人工智能·python·深度学习·神经网络·机器学习·conda·transformer
鸟儿不吃草1 天前
安卓实现左右布局聊天界面
android·开发语言·python
mr_LuoWei20091 天前
类似CASS for autoCAD的平基土石方三维计算工具基本完成
python·三维地形图
alwaysrun1 天前
Python自动提取邮件订阅链接并解析
python·url·邮件·ai提取
何中应1 天前
Conda安装&使用
python·conda·python3.11
无敌昊哥战神1 天前
【LeetCode 37】解数独 (Sudoku Solver) —— 回溯法详解 (Python/C/C++)
c语言·c++·python·算法·leetcode
风流 少年1 天前
Python Web框架:FastAPI
前端·python·fastapi