-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrapeAllAPIJSON.py
More file actions
31 lines (26 loc) · 948 Bytes
/
Copy pathscrapeAllAPIJSON.py
File metadata and controls
31 lines (26 loc) · 948 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
import json
from pymongo import MongoClient
# Constants
BASE_DIRECTORY = "E:\\AllAPIJSON"
FILE_PATTERN = ".json"
# Connect to the local MongoDB instance
client = MongoClient('localhost', 27017)
db = client.mydb
collection = db.AllAPIJSON
def insert_json_to_mongodb(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
# Inserting the JSON data into the MongoDB collection
collection.insert_one(data)
def crawl_and_insert():
# Walk through directory
for dirpath, dirnames, filenames in os.walk(BASE_DIRECTORY):
# Filter out only the JSON files
json_files = [f for f in filenames if f.endswith(FILE_PATTERN)]
for filename in json_files:
file_path = os.path.join(dirpath, filename)
insert_json_to_mongodb(file_path)
print(f"Inserted records from: {file_path}")
if __name__ == "__main__":
crawl_and_insert()