24 lines
947 B
Python
24 lines
947 B
Python
#!/usr/bin/env python3
|
|
"""Scrape example.com title and save as JSON."""
|
|
import json
|
|
import urllib.request
|
|
import re
|
|
|
|
def scrape_title(url):
|
|
try:
|
|
with urllib.request.urlopen(url, timeout=10) as response:
|
|
html = response.read().decode('utf-8')
|
|
match = re.search(r'<title[^>]*>(.*?)</title>', html, re.IGNORECASE | re.DOTALL)
|
|
if match:
|
|
title = match.group(1).strip()
|
|
return {"url": url, "title": title, "status": "success"}
|
|
return {"url": url, "title": None, "status": "no_title"}
|
|
except Exception as e:
|
|
return {"url": url, "title": None, "status": "error", "error": str(e)}
|
|
|
|
if __name__ == "__main__":
|
|
result = scrape_title("https://example.com")
|
|
output_path = "/root/.hermes/wizard-outputs/spark/scrape_result.json"
|
|
with open(output_path, "w") as f:
|
|
json.dump(result, f, indent=2)
|
|
print(f"Result saved to {output_path}") |