42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
import requests
|
|
from bs4 import BeautifulSoup
|
|
|
|
def fetch_title(url):
|
|
try:
|
|
resp = requests.get(url, timeout=10)
|
|
soup = BeautifulSoup(resp.text, "html.parser")
|
|
return soup.title.string.strip() if soup.title else "No title found"
|
|
except Exception as e:
|
|
return f"Error: {e}"
|
|
|
|
def fetch_meta_description(url):
|
|
try:
|
|
resp = requests.get(url, timeout=10)
|
|
soup = BeautifulSoup(resp.text, "html.parser")
|
|
meta = soup.find("meta", attrs={"name": "description"})
|
|
return meta.get("content", "No description found") if meta else "No description found"
|
|
except Exception as e:
|
|
return f"Error: {e}"
|
|
|
|
def fetch_all(url):
|
|
try:
|
|
resp = requests.get(url, timeout=10)
|
|
resp.raise_for_status()
|
|
soup = BeautifulSoup(resp.text, "html.parser")
|
|
title = soup.title.string.strip() if soup.title else "No title"
|
|
meta = soup.find("meta", attrs={"name": "description"})
|
|
description = meta.get("content", "No description") if meta else "No description"
|
|
return {"url": url, "title": title, "description": description, "status_code": resp.status_code}
|
|
except Exception as e:
|
|
return {"url": url, "error": str(e)}
|
|
|
|
if __name__ == "__main__":
|
|
import sys
|
|
url = sys.argv[1] if len(sys.argv) > 1 else "https://example.com"
|
|
result = fetch_all(url)
|
|
print(f"URL: {result.get(url)}")
|
|
print(f"Title: {result.get(title)}")
|
|
print(f"Description: {result.get(description)}")
|
|
print(f"Status: {result.get(status_code, result.get("error", "?"))}")
|