Aider: 初始提交 - 网页爬虫工具
This commit is contained in:
@@ -0,0 +1,18 @@
|
|||||||
|
# Web Scraper
|
||||||
|
|
||||||
|
A simple web scraping tool that fetches page titles and meta descriptions.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install requests beautifulsoup4
|
||||||
|
python web_scraper.py https://example.com
|
||||||
|
```
|
||||||
|
|
||||||
|
## Functions
|
||||||
|
|
||||||
|
- `fetch_title(url)` - Get page title
|
||||||
|
- `fetch_meta_description(url)` - Get meta description
|
||||||
|
- `fetch_all(url)` - Get all info as dict
|
||||||
|
|
||||||
|
Generated by Aider AI
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
requests
|
||||||
|
beautifulsoup4
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
#!/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", "?"))}")
|
||||||
Reference in New Issue
Block a user