File size: 1,128 Bytes
70d956a |
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 |
import requests
from bs4 import BeautifulSoup
from urllib.parse import urlparse
def scrape_webpage(url: str) -> dict:
"""
Herhangi bir web sayfasından başlık ve içerik çeker."""
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
try:
response = requests.get(url , headers = headers , timeout=10)
response.raise_for_status() # HTTP hatalarını kontrol et
except Exception as e:
raise Exception(f"Web sayfası alınamadı: {e}")
soup = BeautifulSoup(response.text , "html.parser")
# Sayfa Başlığı
title_tag = soup.find("title")
title = title_tag.get_text(strip=True) if title_tag else "Başlık bulunamadı"
# Sayfa İçeriği
paragraphs = soup.find_all("p")
content = "\n".join([p.get_text(strip = True) for p in paragraphs if len(p.get_text(strip = True))>30])
if not content:
raise Exception("Sayfa içeriği bulunamadı")
return{
"title":title,
"content":content
} |