Commit
·
0883890
1
Parent(s):
a2c2207
Tool for scraping url endpoints
Browse files- Dockerfile +1 -1
- scrapeAPI.py +124 -0
Dockerfile
CHANGED
@@ -53,4 +53,4 @@ RUN python -m playwright install chromium
|
|
53 |
EXPOSE 7860
|
54 |
|
55 |
# Run the FastAPI application
|
56 |
-
CMD ["python", "-m", "uvicorn", "
|
|
|
53 |
EXPOSE 7860
|
54 |
|
55 |
# Run the FastAPI application
|
56 |
+
CMD ["python", "-m", "uvicorn", "scrapeAPI:app", "--host", "0.0.0.0", "--port", "7860"]
|
scrapeAPI.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, Query
|
2 |
+
from playwright.async_api import async_playwright
|
3 |
+
from urllib.parse import urljoin, urlparse
|
4 |
+
from typing import List, Set
|
5 |
+
import re
|
6 |
+
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
# In-memory cache for JS URLs
|
10 |
+
js_cache: Set[str] = set()
|
11 |
+
|
12 |
+
# Regex for extracting API-like URLs
|
13 |
+
def extract_possible_endpoints(text: str) -> List[str]:
|
14 |
+
pattern = re.compile(r'https?://[^\s"\'<>]+')
|
15 |
+
urls = pattern.findall(text)
|
16 |
+
return list(set([
|
17 |
+
url for url in urls if '/api/' in url or re.search(r'\.(json|php|xml|ajax|aspx|jsp)', url)
|
18 |
+
]))
|
19 |
+
|
20 |
+
# Extract all internal links from a page
|
21 |
+
async def extract_internal_links(page, base_url: str) -> List[str]:
|
22 |
+
anchors = await page.eval_on_selector_all('a[href]', 'els => els.map(el => el.href)')
|
23 |
+
domain = urlparse(base_url).netloc
|
24 |
+
internal_links = [
|
25 |
+
link for link in anchors
|
26 |
+
if urlparse(link).netloc == domain
|
27 |
+
]
|
28 |
+
return list(set(internal_links))
|
29 |
+
|
30 |
+
|
31 |
+
async def scrape_page_for_endpoints(page, url: str) -> List[str]:
|
32 |
+
found_endpoints = []
|
33 |
+
|
34 |
+
try:
|
35 |
+
await page.goto(url, timeout=60000)
|
36 |
+
await page.wait_for_timeout(2000)
|
37 |
+
|
38 |
+
# --- Log network requests ---
|
39 |
+
network_logs = []
|
40 |
+
|
41 |
+
def handle_request(req):
|
42 |
+
network_logs.append(req.url)
|
43 |
+
|
44 |
+
page.on("request", handle_request)
|
45 |
+
|
46 |
+
# --- JS File Endpoint Extraction ---
|
47 |
+
js_urls = await page.eval_on_selector_all(
|
48 |
+
'script[src]',
|
49 |
+
"elements => elements.map(el => el.src)"
|
50 |
+
)
|
51 |
+
|
52 |
+
js_based_endpoints = []
|
53 |
+
|
54 |
+
for js_url in js_urls:
|
55 |
+
if js_url in js_cache:
|
56 |
+
continue
|
57 |
+
js_cache.add(js_url)
|
58 |
+
|
59 |
+
try:
|
60 |
+
response = await page.request.get(js_url)
|
61 |
+
if response.ok:
|
62 |
+
body = await response.text()
|
63 |
+
js_based_endpoints.extend(extract_possible_endpoints(body))
|
64 |
+
except:
|
65 |
+
continue
|
66 |
+
|
67 |
+
# Extract from network requests
|
68 |
+
network_endpoints = extract_possible_endpoints('\n'.join(network_logs))
|
69 |
+
|
70 |
+
# Combine all
|
71 |
+
found_endpoints = list(set(js_based_endpoints + network_endpoints))
|
72 |
+
|
73 |
+
except Exception as e:
|
74 |
+
print(f"[!] Failed to scrape {url}: {e}")
|
75 |
+
|
76 |
+
return found_endpoints
|
77 |
+
|
78 |
+
|
79 |
+
@app.get("/scrape-api-endpoints")
|
80 |
+
async def scrape_api_endpoints(
|
81 |
+
website: str = Query(..., description="Website URL to scrape"),
|
82 |
+
max_depth: int = Query(1, description="Max depth of link crawling (1 = base page only)")
|
83 |
+
):
|
84 |
+
try:
|
85 |
+
visited = set()
|
86 |
+
all_endpoints = []
|
87 |
+
|
88 |
+
async with async_playwright() as p:
|
89 |
+
browser = await p.chromium.launch(headless=True)
|
90 |
+
context = await browser.new_context()
|
91 |
+
page = await context.new_page()
|
92 |
+
|
93 |
+
queue = [(website, 0)]
|
94 |
+
|
95 |
+
while queue:
|
96 |
+
current_url, depth = queue.pop(0)
|
97 |
+
if current_url in visited or depth > max_depth:
|
98 |
+
continue
|
99 |
+
visited.add(current_url)
|
100 |
+
|
101 |
+
print(f"[+] Scraping: {current_url}")
|
102 |
+
endpoints = await scrape_page_for_endpoints(page, current_url)
|
103 |
+
all_endpoints.extend(endpoints)
|
104 |
+
|
105 |
+
if depth < max_depth:
|
106 |
+
try:
|
107 |
+
internal_links = await extract_internal_links(page, website)
|
108 |
+
for link in internal_links:
|
109 |
+
if link not in visited:
|
110 |
+
queue.append((link, depth + 1))
|
111 |
+
except Exception as e:
|
112 |
+
print(f"[!] Link extraction failed: {e}")
|
113 |
+
|
114 |
+
await browser.close()
|
115 |
+
|
116 |
+
return {
|
117 |
+
"website": website,
|
118 |
+
"pages_visited": len(visited),
|
119 |
+
"total_endpoints_found": len(set(all_endpoints)),
|
120 |
+
"api_endpoints": list(set(all_endpoints)),
|
121 |
+
}
|
122 |
+
|
123 |
+
except Exception as e:
|
124 |
+
raise HTTPException(status_code=500, detail=f"Scraping failed: {str(e)}")
|