-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
94 lines (72 loc) · 2.73 KB
/
main.py
File metadata and controls
94 lines (72 loc) · 2.73 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# main.py
"""NH SOS Scraper - Main entry point"""
import asyncio
import os
import sys
import pandas as pd
import config
import scraper
from processor import process_scraped_data
def print_header():
"""Print application header."""
print("\n" + "=" * 80)
print("NH SOS Multi-Term Scraper v6")
print(f"Search terms: {', '.join(config.SEARCH_TERMS)}")
print("Matching: Exact matches only (case-insensitive)")
print("Output: progress.csv, matched.csv, final_data.csv")
print("=" * 80 + "\n")
def check_progress_exists():
"""Check if progress.csv exists."""
return os.path.exists(config.PROGRESS_FILE)
def check_completion_status(state):
"""Check if scraping is complete for all terms."""
if not state:
return False
for term in config.SEARCH_TERMS:
completed = len(state.completed_pages.get(term, set()))
total = state.total_pages.get(term)
if total is None or completed < total:
return False
return True
def print_search_term_summary(df):
"""Print summary of businesses by search term."""
if 'search_term' in df.columns:
print("\nBusinesses by search term:")
print(df['search_term'].value_counts())
async def main():
"""Main application entry point."""
print_header()
# Check if we should skip scraping and go directly to processing
if check_progress_exists() and len(sys.argv) > 1 and sys.argv[1] == '--process-only':
print("Processing mode: Using existing progress.csv")
df = pd.read_csv(config.PROGRESS_FILE)
print(f"\nLoaded {len(df)} businesses from progress.csv")
print_search_term_summary(df)
process_scraped_data(df)
return
state = None
try:
# Run the scraper
state = await scraper.run()
except KeyboardInterrupt:
print("\nScraping interrupted by user")
except Exception as e:
print(f"\n\nUnexpected error: {e}")
print("Progress has been saved - you can resume after fixing the issue")
return
# Process results if scraping is complete
if state:
scraper.print_final_summary(state)
if check_completion_status(state) and os.path.exists(config.PROGRESS_FILE):
# Load scraped data
df = pd.read_csv(config.PROGRESS_FILE)
print(f"\nTotal unique businesses scraped: {len(df)}")
print_search_term_summary(df)
# Process the data
process_scraped_data(df)
else:
print("\nScraping not complete. Resume to continue collecting data.")
print("Or run with --process-only flag to process existing data:")
print(" python main.py --process-only")
if __name__ == "__main__":
asyncio.run(main())