Spaces:
Sleeping
Sleeping
Commit ·
1c02240
1
Parent(s): 971fa6c
feat: add requirements.txt and base_scraper.py - Day 2
Browse files- ai/.gitkeep +0 -0
- api/.gitkeep +0 -0
- blockchain/.gitkeep +0 -0
- docs/.gitkeep +0 -0
- frontend/.gitkeep +0 -0
- graph/.gitkeep +0 -0
- processing/.gitkeep +0 -0
- requirements.txt +13 -0
- scrapers/.gitkeep +0 -0
- scrapers/base_scraper.py +51 -0
- tests/.gitkeep +0 -0
ai/.gitkeep
ADDED
|
File without changes
|
api/.gitkeep
ADDED
|
File without changes
|
blockchain/.gitkeep
ADDED
|
File without changes
|
docs/.gitkeep
ADDED
|
File without changes
|
frontend/.gitkeep
ADDED
|
File without changes
|
graph/.gitkeep
ADDED
|
File without changes
|
processing/.gitkeep
ADDED
|
File without changes
|
requirements.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
requests==2.31.0
|
| 2 |
+
beautifulsoup4==4.12.2
|
| 3 |
+
feedparser==6.0.10
|
| 4 |
+
pdfplumber==0.10.3
|
| 5 |
+
loguru==0.7.2
|
| 6 |
+
python-dotenv==1.0.0
|
| 7 |
+
pandas==2.1.0
|
| 8 |
+
lxml==4.9.3
|
| 9 |
+
fake-useragent==1.3.0
|
| 10 |
+
schedule==1.2.0
|
| 11 |
+
fastapi==0.104.0
|
| 12 |
+
uvicorn==0.24.0
|
| 13 |
+
neo4j==5.14.0
|
scrapers/.gitkeep
ADDED
|
File without changes
|
scrapers/base_scraper.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
BharatGraph - Base Scraper
|
| 3 |
+
All scrapers inherit from this class.
|
| 4 |
+
"""
|
| 5 |
+
import time
|
| 6 |
+
import requests
|
| 7 |
+
from loguru import logger
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
class BaseScraper:
|
| 11 |
+
def __init__(self, name, delay=2.0):
|
| 12 |
+
self.name = name
|
| 13 |
+
self.delay = delay
|
| 14 |
+
self.session = requests.Session()
|
| 15 |
+
self.session.headers.update({
|
| 16 |
+
"User-Agent": "BharatGraph Civic Research Tool (public data only)"
|
| 17 |
+
})
|
| 18 |
+
logger.add(f"logs/{name}.log", rotation="10 MB")
|
| 19 |
+
|
| 20 |
+
def get_json(self, url, params=None):
|
| 21 |
+
try:
|
| 22 |
+
logger.info(f"[{self.name}] Fetching: {url}")
|
| 23 |
+
resp = self.session.get(url, params=params, timeout=30)
|
| 24 |
+
resp.raise_for_status()
|
| 25 |
+
time.sleep(self.delay)
|
| 26 |
+
return resp.json()
|
| 27 |
+
except Exception as e:
|
| 28 |
+
logger.error(f"[{self.name}] Error: {e}")
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
+
def get_html(self, url, params=None):
|
| 32 |
+
try:
|
| 33 |
+
logger.info(f"[{self.name}] Fetching HTML: {url}")
|
| 34 |
+
resp = self.session.get(url, params=params, timeout=30)
|
| 35 |
+
resp.raise_for_status()
|
| 36 |
+
time.sleep(self.delay)
|
| 37 |
+
return resp.text
|
| 38 |
+
except Exception as e:
|
| 39 |
+
logger.error(f"[{self.name}] Error: {e}")
|
| 40 |
+
return None
|
| 41 |
+
|
| 42 |
+
def save_json(self, data, filepath):
|
| 43 |
+
import json
|
| 44 |
+
os.makedirs(os.path.dirname(filepath), exist_ok=True)
|
| 45 |
+
with open(filepath, "w", encoding="utf-8") as f:
|
| 46 |
+
json.dump(data, f, indent=2, ensure_ascii=False)
|
| 47 |
+
logger.info(f"[{self.name}] Saved: {filepath}")
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
s = BaseScraper("test")
|
| 51 |
+
print("BaseScraper ready.")
|
tests/.gitkeep
ADDED
|
File without changes
|