File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -10,7 +10,20 @@ Email: ctrlf4@yeah.net
1010
1111## 快速开始
1212
13- ### 1. 启用 CSV Pipeline
13+ ### 1. 配置 CSV 保存路径(可选)
14+
15+ 在 ` feapder/setting.py ` 或项目的 ` setting.py ` 中配置:
16+
17+ ``` python
18+ # CSV 文件保存路径
19+ CSV_EXPORT_PATH = " data/csv" # 相对路径(默认)
20+ # 或
21+ CSV_EXPORT_PATH = " /Users/xxx/exports/csv" # 绝对路径
22+ ```
23+
24+ 如果不设置,默认使用 ` data/csv ` (相对于运行目录)。
25+
26+ ### 2. 启用 CSV Pipeline
1427
1528在 ` feapder/setting.py ` 中的 ` ITEM_PIPELINES ` 中添加 ` CsvPipeline ` :
1629
@@ -22,7 +35,7 @@ ITEM_PIPELINES = [
2235]
2336```
2437
25- ### 2 . 定义数据项
38+ ### 3 . 定义数据项
2639
2740``` python
2841from feapder.network.item import Item
@@ -34,7 +47,7 @@ class ProductItem(Item):
3447 pass
3548```
3649
37- ### 3 . 在爬虫中使用
50+ ### 4 . 在爬虫中使用
3851
3952``` python
4053import feapder
@@ -49,7 +62,7 @@ class MySpider(feapder.AirSpider):
4962 yield item # 自动保存为 CSV
5063```
5164
52- ### 4 . 查看输出
65+ ### 5 . 查看输出
5366
5467爬虫运行后,CSV 文件会保存在 ` data/csv/ ` 目录下:
5568
Original file line number Diff line number Diff line change @@ -38,15 +38,25 @@ class CsvPipeline(BasePipeline):
3838 # 确保跨批次、跨线程的字段顺序一致
3939 _table_fieldnames = {}
4040
41- def __init__ (self , csv_dir = "data/csv" ):
41+ def __init__ (self , csv_dir = None ):
4242 """
4343 初始化CSV Pipeline
4444
4545 Args:
46- csv_dir: CSV文件保存目录,默认为 data/csv
46+ csv_dir: CSV文件保存目录
47+ - 如果不传,从 setting.CSV_EXPORT_PATH 读取
48+ - 支持相对路径(如 "data/csv")
49+ - 支持绝对路径(如 "/Users/xxx/exports/csv")
4750 """
4851 super ().__init__ ()
49- self .csv_dir = csv_dir
52+
53+ # 如果未传入参数,从配置文件读取
54+ if csv_dir is None :
55+ import feapder .setting as setting
56+ csv_dir = setting .CSV_EXPORT_PATH
57+
58+ # 支持绝对路径和相对路径,统一转换为绝对路径
59+ self .csv_dir = os .path .abspath (csv_dir )
5060 self ._ensure_csv_dir_exists ()
5161
5262 def _ensure_csv_dir_exists (self ):
Original file line number Diff line number Diff line change 4949EXPORT_DATA_MAX_FAILED_TIMES = 10 # 导出数据时最大的失败次数,包括保存和更新,超过这个次数报警
5050EXPORT_DATA_MAX_RETRY_TIMES = 10 # 导出数据时最大的重试次数,包括保存和更新,超过这个次数则放弃重试
5151
52+ # CSV Pipeline 配置
53+ CSV_EXPORT_PATH = "data/csv" # CSV文件保存路径,支持相对路径和绝对路径
54+ # 相对路径:相对于运行目录(如 "data/csv")
55+ # 绝对路径:完整路径(如 "/Users/xxx/exports/csv")
56+
5257# 爬虫相关
5358# COLLECTOR
5459COLLECTOR_TASK_COUNT = 32 # 每次获取任务数量,追求速度推荐32
You can’t perform that action at this time.
0 commit comments