Spaces:
Sleeping
Sleeping
File size: 3,630 Bytes
245508e | 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | import requests
from bs4 import BeautifulSoup
from datetime import datetime
def fetch_news_from_aljazeera():
"""جلب الأخبار من موقع الجزيرة نت"""
url = 'https://www.aljazeera.net/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
try:
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
news_items = []
# البحث عن الأخبار في الصفحة
articles = soup.find_all(['article', 'div'], class_=lambda x: x and ('news' in x or 'article' in x))
if not articles:
# محاولة بديلة: البحث عن الروابط التي تحتوي على أخبار
all_links = soup.find_all('a')
for link in all_links[:30]:
title = link.get_text().strip()
if len(title) > 20 and title: # عناوين طويلة بما فيه الكفاية
href = link.get('href', '')
if href and not href.startswith('#'):
if not href.startswith('http'):
href = 'https://www.aljazeera.net' + href if href.startswith('/') else 'https://www.aljazeera.net/' + href
news_items.append({
'title': title[:200],
'link': href,
'summary': title[:150],
'source': 'الجزيرة نت',
'date': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
})
# إذا لم نجد أي شيء، نضيف أخبارًا تجريبية
if not news_items:
news_items = get_sample_news()
return news_items[:20] # آخر 20 خبر
except Exception as e:
print(f"خطأ في جلب الأخبار: {e}")
return get_sample_news()
def get_sample_news():
"""أخبار تجريبية في حالة فشل الاتصال"""
return [
{
'title': 'الذكاء الاصطناعي يحدث ثورة في محركات البحث',
'link': 'https://example.com/news1',
'summary': 'تقنيات جديدة في مجال البحث تتيح نتائج أكثر دقة وسرعة',
'source': 'وكالة الأنباء التقنية',
'date': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
},
{
'title': 'إطلاق محرك بحث جديد للصور والفيديوهات',
'link': 'https://example.com/news2',
'summary': 'محرك بحث متخصص يوفر نتائج دقيقة للصور ومقاطع الفيديو',
'source': 'بوابة التكنولوجيا',
'date': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
},
{
'title': 'تحديثات جديدة في عالم الأخبار الرقمية',
'link': 'https://example.com/news3',
'summary': 'منصات الأخبار تتجه نحو استخدام الذكاء الاصطناعي لتحسين المحتوى',
'source': 'مركز الإعلام الرقمي',
'date': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
}
]
if __name__ == '__main__':
news = fetch_news_from_aljazeera()
for item in news:
print(f"العنوان: {item['title']}") |