-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusms-scrape.py
More file actions
100 lines (80 loc) · 2.94 KB
/
usms-scrape.py
File metadata and controls
100 lines (80 loc) · 2.94 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
import os
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin, urlparse
import time
# Create output directory
OUTPUT_DIR = "usmarshals_pdfs"
os.makedirs(OUTPUT_DIR, exist_ok=True)
# List of URLs to scrape
URLS = [
"https://www.usmarshals.gov/resources/publications",
"https://www.usmarshals.gov/resources/publications/policy-directives",
"https://www.usmarshals.gov/resources/fact-sheets",
"https://www.usmarshals.gov/resources/guideline"
]
def download_pdf(pdf_url, output_dir):
"""Download a PDF file from the given URL"""
try:
# Extract filename from URL
filename = os.path.basename(urlparse(pdf_url).path)
# Skip if already downloaded
filepath = os.path.join(output_dir, filename)
if os.path.exists(filepath):
print(f"Already exists: {filename}")
return
# Download the PDF
print(f"Downloading: {filename}")
response = requests.get(pdf_url, timeout=30)
response.raise_for_status()
# Save the PDF
with open(filepath, 'wb') as f:
f.write(response.content)
print(f"Saved: {filename}")
except Exception as e:
print(f"Error downloading {pdf_url}: {str(e)}")
def scrape_pdfs_from_page(url):
"""Scrape all PDF links from a given page"""
try:
print(f"\nScraping: {url}")
response = requests.get(url, timeout=30)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
# Find all links
pdf_links = []
for link in soup.find_all('a', href=True):
href = link['href']
# Check if it's a PDF link
if href.endswith('.pdf'):
# Convert relative URLs to absolute
full_url = urljoin(url, href)
pdf_links.append(full_url)
print(f"Found {len(pdf_links)} PDF(s)")
return pdf_links
except Exception as e:
print(f"Error scraping {url}: {str(e)}")
return []
def main():
"""Main function to scrape all URLs and download PDFs"""
print("US Marshals PDF Scraper")
print("=" * 50)
all_pdfs = []
# Scrape each URL
for url in URLS:
pdf_links = scrape_pdfs_from_page(url)
all_pdfs.extend(pdf_links)
time.sleep(1) # Be polite to the server
# Remove duplicates
all_pdfs = list(set(all_pdfs))
print(f"\nTotal unique PDFs found: {len(all_pdfs)}")
print("=" * 50)
# Download all PDFs
for i, pdf_url in enumerate(all_pdfs, 1):
print(f"\n[{i}/{len(all_pdfs)}]")
download_pdf(pdf_url, OUTPUT_DIR)
time.sleep(0.5) # Be polite to the server
print("\n" + "=" * 50)
print(f"Download complete! PDFs saved to: {OUTPUT_DIR}")
print(f"Total files downloaded: {len(os.listdir(OUTPUT_DIR))}")
if __name__ == "__main__":
main()