Spaces:
Sleeping
Sleeping
| 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']}") |