Repository navigation
Expand file tree
/
Copy pathrun_scraper.py
More file actions
256 lines (199 loc) · 8.22 KB
/
Copy pathrun_scraper.py
File metadata and controls
256 lines (199 loc) · 8.22 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python3
"""
Standalone LinkedIn Job Scraper Script
Run directly from terminal: python run_scraper.py
Phase 1: Scrape job URLs (infinite scroll)
Phase 2: Scrape job details from URLs
"""
import asyncio
import logging
import sys
from datetime import datetime
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(message)s", datefmt="%H:%M:%S"
)
logger = logging.getLogger(__name__)
async def phase1_scrape_urls(keyword: str, location: str, limit: int = 100):
"""Phase 1: Scrape LinkedIn job URLs using infinite scroll"""
from src.scraper.unified.linkedin.infinite_scroll_scraper import (
scrape_linkedin_urls_infinite_scroll,
)
from src.db.operations import JobStorageOperations
print("\n" + "=" * 60)
print(f"PHASE 1: Scraping {limit} LinkedIn Job URLs")
print(f"Keyword: {keyword} | Location: {location}")
print("Mode: Visible Browser (headless=False)")
print("=" * 60 + "\n")
urls = await scrape_linkedin_urls_infinite_scroll(
keyword=keyword, location=location, limit=limit
)
print(f"\n✅ Phase 1 Complete: {len(urls)} URLs collected")
if urls:
db = JobStorageOperations("data/jobs.db")
stored = db.store_urls(urls)
print(f"📦 Stored {stored} NEW URLs to database")
return urls
async def phase2_scrape_details(platform: str, role: str, batch_size: int = 100):
"""Phase 2: Scrape job details from stored URLs"""
from src.scraper.unified.linkedin.sequential_detail_scraper import (
scrape_job_details_sequential,
)
from src.db.operations import JobStorageOperations
db = JobStorageOperations("data/jobs.db")
# Get unscraped URLs
urls = db.get_unscraped_urls(platform, role, batch_size)
if not urls:
print(f"\n⚠️ No unscraped URLs found for {role} on {platform}")
return []
print("\n" + "=" * 60)
print(f"PHASE 2: Scraping {len(urls)} Job Details")
print(f"Platform: {platform} | Role: {role}")
print("Mode: Visible Browser (headless=False)")
print("=" * 60 + "\n")
results = await scrape_job_details_sequential(
urls=urls, headless=False, prefetch_size=5
)
print(f"\n✅ Phase 2 Complete: {len(results)} jobs scraped successfully")
# Summary
if results:
all_skills = set()
for job in results:
if job.skills:
for s in job.skills.split(","):
all_skills.add(s.strip())
print(f"📊 Unique skills extracted: {len(all_skills)}")
return results
async def full_scrape(
keyword: str, location: str, url_limit: int = 100, detail_batch: int = 100
):
"""Run both phases: URL collection + Detail scraping"""
start_time = datetime.now()
print("\n" + "🚀" * 20)
print("LINKEDIN JOB SCRAPER - FULL RUN")
print(f"Started: {start_time.strftime('%Y-%m-%d %H:%M:%S')}")
print("🚀" * 20 + "\n")
# Phase 1: Collect URLs
urls = await phase1_scrape_urls(keyword, location, url_limit)
# Phase 2: Scrape details
if urls:
jobs = await phase2_scrape_details("linkedin", keyword, detail_batch)
else:
# Try scraping existing unscraped URLs
jobs = await phase2_scrape_details("linkedin", keyword, detail_batch)
end_time = datetime.now()
duration = (end_time - start_time).total_seconds()
print("\n" + "=" * 60)
print("SCRAPING COMPLETE")
print("=" * 60)
print(f"Duration: {duration:.1f} seconds")
print(f"URLs Collected: {len(urls) if urls else 0}")
print(f"Jobs Scraped: {len(jobs) if jobs else 0}")
print("=" * 60 + "\n")
return urls, jobs
def show_menu():
"""Show interactive menu"""
print("\n" + "=" * 60)
print("LINKEDIN JOB SCRAPER")
print("=" * 60)
print("1. Full Scrape (URLs + Details)")
print("2. Phase 1 Only (Scrape URLs)")
print("3. Phase 2 Only (Scrape Details from existing URLs)")
print("4. Check Database Status")
print("5. Exit")
print("=" * 60)
return input("Select option (1-5): ").strip()
def check_db_status():
"""Show current database status"""
import sqlite3
conn = sqlite3.connect("data/jobs.db")
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM job_urls")
total_urls = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM job_urls WHERE scraped = 0")
unscraped = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM jobs")
total_jobs = cursor.fetchone()[0]
print("\n" + "=" * 60)
print("DATABASE STATUS")
print("=" * 60)
print(f"Total URLs: {total_urls}")
print(f"Unscraped URLs: {unscraped}")
print(f"Total Jobs: {total_jobs}")
cursor.execute("""
SELECT actual_role, COUNT(*)
FROM job_urls
WHERE scraped = 0
GROUP BY actual_role
""")
roles = cursor.fetchall()
if roles:
print(f"\nUnscraped by Role:")
for role, count in roles:
print(f" {role}: {count}")
conn.close()
print("=" * 60 + "\n")
def main():
"""Main entry point"""
if len(sys.argv) > 1:
# Command line arguments
cmd = sys.argv[1].lower()
if cmd == "full":
keyword = sys.argv[2] if len(sys.argv) > 2 else "Data Engineer"
location = sys.argv[3] if len(sys.argv) > 3 else "India"
limit = int(sys.argv[4]) if len(sys.argv) > 4 else 100
asyncio.run(full_scrape(keyword, location, limit, limit))
elif cmd == "urls":
keyword = sys.argv[2] if len(sys.argv) > 2 else "Data Engineer"
location = sys.argv[3] if len(sys.argv) > 3 else "India"
limit = int(sys.argv[4]) if len(sys.argv) > 4 else 100
asyncio.run(phase1_scrape_urls(keyword, location, limit))
elif cmd == "details":
role = sys.argv[2] if len(sys.argv) > 2 else "Data Engineer"
batch = int(sys.argv[3]) if len(sys.argv) > 3 else 100
asyncio.run(phase2_scrape_details("linkedin", role, batch))
elif cmd == "status":
check_db_status()
else:
print("Usage:")
print(" python run_scraper.py full [keyword] [location] [limit]")
print(" python run_scraper.py urls [keyword] [location] [limit]")
print(" python run_scraper.py details [role] [batch_size]")
print(" python run_scraper.py status")
else:
# Interactive menu
while True:
choice = show_menu()
if choice == "1":
keyword = (
input("Enter keyword (default: Data Engineer): ").strip()
or "Data Engineer"
)
location = input("Enter location (default: India): ").strip() or "India"
limit = input("Enter URL limit (default: 100): ").strip() or "100"
asyncio.run(full_scrape(keyword, location, int(limit), int(limit)))
elif choice == "2":
keyword = (
input("Enter keyword (default: Data Engineer): ").strip()
or "Data Engineer"
)
location = input("Enter location (default: India): ").strip() or "India"
limit = input("Enter URL limit (default: 100): ").strip() or "100"
asyncio.run(phase1_scrape_urls(keyword, location, int(limit)))
elif choice == "3":
role = (
input("Enter role (default: Data Engineer): ").strip()
or "Data Engineer"
)
batch = input("Enter batch size (default: 100): ").strip() or "100"
asyncio.run(phase2_scrape_details("linkedin", role, int(batch)))
elif choice == "4":
check_db_status()
elif choice == "5":
print("Goodbye!")
break
else:
print("Invalid option. Please select 1-5.")
if __name__ == "__main__":
print("Invalid option. Please select 1-5.")
if __name__ == '__main__':
main()