Repository navigation
Expand file tree
/
Copy pathscraper.py
More file actions
70 lines (56 loc) · 2.25 KB
/
Copy pathscraper.py
File metadata and controls
70 lines (56 loc) · 2.25 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
from llm_handler import LLMHandler
from page_inspector import PageInspector
from page_interactor import PageInteractor
from playwright.sync_api import sync_playwright
from urllib.parse import urljoin
class PDFScraper:
def __init__(self, debug=True):
self.debug = debug
self.llm = LLMHandler(debug=debug)
self.inspector = PageInspector(debug=debug)
self.interactor = PageInteractor(debug=debug)
def scrape_website(self, url, max_iterations=20):
pdf_links = []
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
page = browser.new_context().new_page()
try:
page.goto(url, wait_until="networkidle", timeout=60000)
for iteration in range(max_iterations):
# Inspect page
inspection_results = self.inspector.inspect_page(page)
# Get LLM analysis with inspection results
analysis = self.llm.analyze_page(
page.content(),
inspection_results,
f"Iteration {iteration + 1}"
)
if not analysis:
break
# Collect PDF links
pdf_links.extend(self._collect_pdf_links(page, url))
# Try interactions
if not self._try_suggested_actions(page, analysis):
break
finally:
browser.close()
return list(set(pdf_links))
def _collect_pdf_links(self, page, base_url):
"""Collect PDF links from the page"""
links = []
for link in page.query_selector_all('a[href*=".pdf"]'):
href = link.get_attribute('href')
if href:
links.append(urljoin(base_url, href))
return links
def _try_suggested_actions(self, page, analysis):
"""Try actions suggested by LLM"""
actions = sorted(
analysis['actions'],
key=lambda x: x['priority'],
reverse=True
)
for action in actions:
if self.interactor.interact_with_element(page, action):
return True
return False