Update .gitignore and requirements; enhance logging in Playwright scraper
This commit is contained in:
@@ -68,3 +68,4 @@ secrets.json
|
|||||||
.playwright_profile
|
.playwright_profile
|
||||||
|
|
||||||
captured/
|
captured/
|
||||||
|
xhr_captured/
|
||||||
@@ -2,3 +2,4 @@ requests>=2.28.0
|
|||||||
beautifulsoup4>=4.12.0
|
beautifulsoup4>=4.12.0
|
||||||
playwright>=1.40.0
|
playwright>=1.40.0
|
||||||
python-dotenv>=1.0.0
|
python-dotenv>=1.0.0
|
||||||
|
loguru
|
||||||
@@ -22,6 +22,7 @@ import mimetypes
|
|||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from playwright.sync_api import sync_playwright
|
from playwright.sync_api import sync_playwright
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
# Load environment variables from .env in the repository root (if present)
|
# Load environment variables from .env in the repository root (if present)
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
@@ -90,13 +91,17 @@ def main():
|
|||||||
fh.write(body)
|
fh.write(body)
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
capture_types = {t.strip().lower() for t in args.capture_types.split(',') if t.strip()}
|
||||||
|
|
||||||
def on_response(resp):
|
def on_response(resp):
|
||||||
|
# lightweight debug log
|
||||||
|
logging.debug('Response: %s %s', resp.status, resp.url)
|
||||||
try:
|
try:
|
||||||
req = resp.request
|
req = resp.request
|
||||||
rtype = req.resource_type
|
rtype = req.resource_type
|
||||||
if args.domain not in req.url:
|
if args.domain not in req.url:
|
||||||
return
|
return
|
||||||
if rtype not in ('xhr', 'fetch'):
|
if rtype not in capture_types:
|
||||||
return
|
return
|
||||||
|
|
||||||
ts = datetime.now(timezone.utc).strftime('%Y%m%dT%H%M%SZ')
|
ts = datetime.now(timezone.utc).strftime('%Y%m%dT%H%M%SZ')
|
||||||
@@ -111,6 +116,7 @@ def main():
|
|||||||
'timestamp': ts,
|
'timestamp': ts,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Request post data
|
||||||
try:
|
try:
|
||||||
post = req.post_data
|
post = req.post_data
|
||||||
info['request_post_data'] = post
|
info['request_post_data'] = post
|
||||||
@@ -120,9 +126,10 @@ def main():
|
|||||||
except Exception:
|
except Exception:
|
||||||
info['request_post_data'] = None
|
info['request_post_data'] = None
|
||||||
|
|
||||||
|
# Response body (text or binary)
|
||||||
ctype = resp.headers.get('content-type', '')
|
ctype = resp.headers.get('content-type', '')
|
||||||
try:
|
try:
|
||||||
if 'application/json' in ctype or 'text/' in ctype or 'application/javascript' in ctype:
|
if 'application/json' in ctype or ctype.startswith('text/') or 'javascript' in ctype:
|
||||||
txt = resp.text()
|
txt = resp.text()
|
||||||
info['response_text'] = txt
|
info['response_text'] = txt
|
||||||
info['response_body_file'] = None
|
info['response_body_file'] = None
|
||||||
@@ -147,7 +154,7 @@ def main():
|
|||||||
|
|
||||||
json.dump(info, fh, indent=2)
|
json.dump(info, fh, indent=2)
|
||||||
|
|
||||||
logging.info('Captured XHR: %s -> %s', req.url, out_path)
|
logging.info('Captured %s: %s -> %s', rtype, req.url, out_path)
|
||||||
captured.append(out_path)
|
captured.append(out_path)
|
||||||
except Exception:
|
except Exception:
|
||||||
logging.exception('Error in capture on_response')
|
logging.exception('Error in capture on_response')
|
||||||
@@ -233,8 +240,8 @@ def main():
|
|||||||
if args.keep_open:
|
if args.keep_open:
|
||||||
logging.info('Leaving browser open for manual inspection. Close the browser window to finish.')
|
logging.info('Leaving browser open for manual inspection. Close the browser window to finish.')
|
||||||
try:
|
try:
|
||||||
# keep running until user closes browser
|
# keep running until the user presses Enter in the terminal
|
||||||
context.wait_for_event('close')
|
input('Browser is open. Press Enter here to close and exit...')
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user