-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnavy-scrape.py
More file actions
282 lines (221 loc) · 9.44 KB
/
navy-scrape.py
File metadata and controls
282 lines (221 loc) · 9.44 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import os
import time
import random
from urllib.parse import urljoin, urlparse
import requests
try:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
except ImportError:
print("Install: pip install selenium beautifulsoup4 requests")
exit(1)
def setup_realistic_browser():
"""
Configure Chrome with realistic browser fingerprint to avoid detection.
NOW RUNS IN HEADLESS MODE - won't render UI on your screen.
"""
chrome_options = Options()
# HEADLESS MODE - This prevents the browser from rendering on screen
chrome_options.add_argument('--headless=new')
# Anti-detection measures
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
# Realistic window size (still needed for headless rendering)
chrome_options.add_argument('--window-size=1920,1080')
# Realistic user agent
chrome_options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')
# Additional anti-detection and performance settings
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-software-rasterizer')
chrome_options.add_argument('--disable-extensions')
# Language and locale
chrome_options.add_argument('--lang=en-US')
chrome_options.add_experimental_option('prefs', {
'intl.accept_languages': 'en-US,en;q=0.9'
})
driver = webdriver.Chrome(options=chrome_options)
# Remove webdriver property
driver.execute_cdp_cmd('Network.setUserAgentOverride', {
"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
})
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
# Add realistic browser properties
driver.execute_script("""
Object.defineProperty(navigator, 'plugins', {
get: () => [1, 2, 3, 4, 5]
});
Object.defineProperty(navigator, 'languages', {
get: () => ['en-US', 'en']
});
""")
return driver
def get_realistic_headers():
"""
Return comprehensive, realistic browser headers.
"""
return {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Accept-Language': 'en-US,en;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'DNT': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
'Cache-Control': 'max-age=0',
'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'Referer': 'https://www.secnav.navy.mil/'
}
def extract_navy_pdfs(url, output_dir):
"""
Extract PDFs with enhanced anti-detection.
"""
driver = setup_realistic_browser()
all_pdfs = []
try:
print(f"\nLoading: {url}")
driver.get(url)
# Mimic human behavior
print("Simulating human behavior...")
time.sleep(5)
# Random mouse movements and scrolling
driver.execute_script("window.scrollTo(0, 500);")
time.sleep(2)
driver.execute_script("window.scrollTo(0, 1000);")
time.sleep(2)
driver.execute_script("window.scrollTo(0, 0);")
time.sleep(3)
# Wait for content
print("Waiting for SharePoint content...")
time.sleep(15)
# Get HTML
page_html = driver.page_source
soup = BeautifulSoup(page_html, 'html.parser')
# Find all PDF links
all_links = soup.find_all('a', href=True)
print(f"Found {len(all_links)} total links")
for link in all_links:
href = link.get('href', '')
text = link.get_text(strip=True)
if '.pdf' in href.lower():
full_url = urljoin(url, href)
if not any(pdf['url'] == full_url for pdf in all_pdfs):
all_pdfs.append({
'url': full_url,
'name': text if text else os.path.basename(urlparse(full_url).path)
})
print(f" ✓ {text}")
return all_pdfs
finally:
driver.quit()
def download_pdfs(pdf_list, output_dir):
"""Download PDFs with realistic headers."""
if not os.path.exists(output_dir):
os.makedirs(output_dir)
session = requests.Session()
session.headers.update(get_realistic_headers())
for idx, pdf in enumerate(pdf_list, 1):
try:
filename = os.path.basename(pdf['url'].split('?')[0])
filepath = os.path.join(output_dir, filename)
if os.path.exists(filepath):
print(f"[{idx}/{len(pdf_list)}] ⊙ {filename}")
continue
print(f"[{idx}/{len(pdf_list)}] ↓ {filename}")
# Add random delay to mimic human
time.sleep(random.uniform(2, 5))
response = session.get(pdf['url'], timeout=60)
response.raise_for_status()
if not response.content.startswith(b'%PDF'):
print(f" ⚠ Not PDF")
continue
with open(filepath, 'wb') as f:
f.write(response.content)
print(f" ✓ {len(response.content) / 1024:.2f} KB")
except Exception as e:
print(f" ✗ {str(e)}")
def scrape_multiple_urls(urls, base_output_dir):
"""
Scrape multiple URLs and organize PDFs by source.
"""
all_pdfs_by_source = {}
for idx, url in enumerate(urls, 1):
print(f"\n{'='*60}")
print(f"Processing URL {idx}/{len(urls)}")
print(f"{'='*60}")
# Extract source name from URL for organizing
source_name = urlparse(url).path.strip('/').split('/')[-1].replace('.aspx', '')
output_dir = os.path.join(base_output_dir, source_name)
try:
pdfs = extract_navy_pdfs(url, output_dir)
all_pdfs_by_source[source_name] = {
'url': url,
'pdfs': pdfs,
'output_dir': output_dir
}
print(f"\n✓ Found {len(pdfs)} PDFs from {source_name}")
# Human-like delay between URLs (random 10-20 seconds)
if idx < len(urls):
delay = random.uniform(10, 20)
print(f"\nWaiting {delay:.1f}s before next URL...")
time.sleep(delay)
except Exception as e:
print(f"\n✗ Error processing {url}: {str(e)}")
all_pdfs_by_source[source_name] = {
'url': url,
'pdfs': [],
'output_dir': output_dir,
'error': str(e)
}
return all_pdfs_by_source
if __name__ == "__main__":
print("Navy SECNAV - Multi-URL Scraper (HEADLESS MODE)")
print("="*60 + "\n")
# All URLs to scrape
urls_to_scrape = [
'https://www.secnav.navy.mil/doni/notices.aspx',
'https://www.secnav.navy.mil/doni/allinstructions.aspx',
'https://www.secnav.navy.mil/doni/manuals-secnav.aspx',
'https://www.secnav.navy.mil/doni/manuals-opnav.aspx',
'https://www.secnav.navy.mil/doni/navalintelligence.aspx'
]
base_output_dir = 'navy_pdfs'
# Scrape all URLs
results = scrape_multiple_urls(urls_to_scrape, base_output_dir)
# Summary
print(f"\n{'='*60}")
print("SCRAPING SUMMARY")
print(f"{'='*60}\n")
total_pdfs = 0
for source_name, data in results.items():
pdf_count = len(data['pdfs'])
total_pdfs += pdf_count
status = "✓" if pdf_count > 0 else "✗"
print(f"{status} {source_name}: {pdf_count} PDFs")
if 'error' in data:
print(f" Error: {data['error']}")
print(f"\nTotal PDFs found: {total_pdfs}")
# Download prompt
if total_pdfs > 0:
proceed = input("\nDownload all PDFs? (yes/no): ").strip().lower()
if proceed in ['yes', 'y']:
for source_name, data in results.items():
if data['pdfs']:
print(f"\n{'='*60}")
print(f"Downloading from: {source_name}")
print(f"{'='*60}")
download_pdfs(data['pdfs'], data['output_dir'])
else:
print("\n✗ No PDFs found across all sources")