Move policy-analysis reports to output/ subdirectory
This commit is contained in:
@@ -448,7 +448,8 @@ def print_stats(listings, new_items=None, updated_items=None, early_stopped=Fals
|
||||
|
||||
|
||||
def scrape(keyword="塘厦", cate_id="15", sort_field="11",
|
||||
max_scrolls=100, output_dir=".", incremental=False):
|
||||
max_scrolls=100, output_dir="output/法拍",
|
||||
incremental=False, csv_only=False):
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
now_str = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
@@ -463,10 +464,12 @@ def scrape(keyword="塘厦", cate_id="15", sort_field="11",
|
||||
if incremental:
|
||||
existing, known_ids = load_existing(json_path)
|
||||
if not known_ids:
|
||||
print("未找到已有数据,转为首次抓取模式")
|
||||
print("未找到已有 JSON 数据,转为首次抓取模式")
|
||||
incremental = False
|
||||
else:
|
||||
print(f"已有数据: {len(existing)} 条, 已知 {len(known_ids)} 个 paimaiId")
|
||||
if csv_only:
|
||||
print("提示: csv_only 模式下增量更新仍需 JSON 做合并,临时使用后删除")
|
||||
|
||||
new_listings, early_stopped = fetch_listings(
|
||||
keyword, cate_id, sort_field, max_scrolls,
|
||||
@@ -482,9 +485,12 @@ def scrape(keyword="塘厦", cate_id="15", sort_field="11",
|
||||
print(f"合并后总数: {len(merged)} 条")
|
||||
print(f"新增: {len(new_items)} 条 | 更新: {len(updated_items)} 条")
|
||||
|
||||
with open(json_path, "w", encoding="utf-8") as f:
|
||||
json.dump(merged, f, indent=2, ensure_ascii=False)
|
||||
if not csv_only:
|
||||
with open(json_path, "w", encoding="utf-8") as f:
|
||||
json.dump(merged, f, indent=2, ensure_ascii=False)
|
||||
save_csv(merged, csv_path)
|
||||
if csv_only and os.path.exists(json_path):
|
||||
os.remove(json_path)
|
||||
|
||||
log_path = os.path.join(output_dir, f"{keyword}_增量日志.log")
|
||||
with open(log_path, "a", encoding="utf-8") as f:
|
||||
@@ -505,7 +511,8 @@ def scrape(keyword="塘厦", cate_id="15", sort_field="11",
|
||||
f"{ts_to_str(item.get('_startTime'))}\n")
|
||||
|
||||
print(f"\n输出文件:")
|
||||
print(f" JSON: {json_path}")
|
||||
if not csv_only:
|
||||
print(f" JSON: {json_path}")
|
||||
print(f" CSV: {csv_path}")
|
||||
print(f" 日志: {log_path}")
|
||||
|
||||
@@ -517,12 +524,14 @@ def scrape(keyword="塘厦", cate_id="15", sort_field="11",
|
||||
item["_first_seen"] = now_str
|
||||
item["_last_seen"] = now_str
|
||||
|
||||
with open(json_path, "w", encoding="utf-8") as f:
|
||||
json.dump(sorted_listings, f, indent=2, ensure_ascii=False)
|
||||
if not csv_only:
|
||||
with open(json_path, "w", encoding="utf-8") as f:
|
||||
json.dump(sorted_listings, f, indent=2, ensure_ascii=False)
|
||||
save_csv(sorted_listings, csv_path)
|
||||
|
||||
print(f"\n输出文件:")
|
||||
print(f" JSON: {json_path}")
|
||||
if not csv_only:
|
||||
print(f" JSON: {json_path}")
|
||||
print(f" CSV: {csv_path}")
|
||||
|
||||
print_stats(sorted_listings)
|
||||
@@ -535,9 +544,11 @@ if __name__ == "__main__":
|
||||
parser.add_argument("--cate-id", default="15", help="类目ID (默认: 15=法拍房)")
|
||||
parser.add_argument("--sort-field", default="11", help="排序 (默认: 11=最新发布)")
|
||||
parser.add_argument("--max-scrolls", type=int, default=100, help="最大滚动次数")
|
||||
parser.add_argument("--output-dir", default=".", help="输出目录")
|
||||
parser.add_argument("--output-dir", default="output/法拍", help="输出目录 (默认: output/法拍)")
|
||||
parser.add_argument("--incremental", "-i", action="store_true",
|
||||
help="增量更新:只下载新房源,遇到已知项即停止")
|
||||
parser.add_argument("--csv-only", action="store_true",
|
||||
help="只输出 CSV,不保存 JSON(增量模式下 JSON 临时使用后删除)")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
@@ -548,4 +559,5 @@ if __name__ == "__main__":
|
||||
max_scrolls=args.max_scrolls,
|
||||
output_dir=args.output_dir,
|
||||
incremental=args.incremental,
|
||||
csv_only=args.csv_only,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user