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
相关推荐
Flittly1 分钟前
【LangGraph新手村系列】(5)时间旅行:浏览历史、分叉时间线与修改过去
python·langchain
2301_7820404543 分钟前
CSS Flex布局中如何实现导航栏与Logo的左右分布_利用justify-content- space-between
jvm·数据库·python
yaoxin5211231 小时前
400. Java 文件操作基础 - 使用 Buffered Stream I/O 读取文本文件
java·开发语言·python
用户8356290780511 小时前
使用 Python 自动创建 Excel 折线图
后端·python
小白学大数据2 小时前
面向大规模爬取:Python 全站链接爬虫优化(过滤 + 断点续爬)
开发语言·爬虫·python
WL_Aurora2 小时前
【每日一题】贪心
python·算法
IT策士3 小时前
Python 中间件系列:redis 深入浅出
redis·python·中间件
Dxy12393102163 小时前
Python Pillow库:`img.format`与`img.mode`的区别详解
开发语言·python·pillow
༒࿈南林࿈༒4 小时前
刺猬猫小说下载
python·js逆向
.柒宇.4 小时前
AI-Agent入门实战-AI私厨
人工智能·python·langchain·agent·fastapi