Create scraper.py
Browse files- scraper.py +140 -0
scraper.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
import feedparser
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
class YahooFinanceScraper:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.headers = {
|
| 10 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
| 11 |
+
}
|
| 12 |
+
self.base_url = "https://finance.yahoo.com"
|
| 13 |
+
|
| 14 |
+
def get_latest_news(self, symbol="", max_articles=10):
|
| 15 |
+
"""
|
| 16 |
+
ดึงข่าวล่าสุดจาก Yahoo Finance
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
symbol: ticker symbol (เช่น AAPL, TSLA) หรือเว้นว่างสำหรับข่าวทั่วไป
|
| 20 |
+
max_articles: จำนวนข่าวที่ต้องการ
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
list: รายการข่าว [{title, link, summary, published}]
|
| 24 |
+
"""
|
| 25 |
+
news_list = []
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
if symbol:
|
| 29 |
+
# ดึงข่าวเฉพาะหุ้น
|
| 30 |
+
url = f"{self.base_url}/quote/{symbol}"
|
| 31 |
+
response = requests.get(url, headers=self.headers, timeout=10)
|
| 32 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 33 |
+
|
| 34 |
+
# หา news section
|
| 35 |
+
news_items = soup.find_all('li', class_='stream-item', limit=max_articles)
|
| 36 |
+
|
| 37 |
+
for item in news_items:
|
| 38 |
+
try:
|
| 39 |
+
title_tag = item.find('h3')
|
| 40 |
+
link_tag = item.find('a')
|
| 41 |
+
summary_tag = item.find('p')
|
| 42 |
+
time_tag = item.find('time')
|
| 43 |
+
|
| 44 |
+
if title_tag and link_tag:
|
| 45 |
+
news_list.append({
|
| 46 |
+
'title': title_tag.get_text(strip=True),
|
| 47 |
+
'link': link_tag.get('href', ''),
|
| 48 |
+
'summary': summary_tag.get_text(strip=True) if summary_tag else '',
|
| 49 |
+
'published': time_tag.get_text(strip=True) if time_tag else 'N/A'
|
| 50 |
+
})
|
| 51 |
+
except Exception as e:
|
| 52 |
+
continue
|
| 53 |
+
|
| 54 |
+
else:
|
| 55 |
+
# ดึงข่าวทั่วไปผ่าน RSS Feed
|
| 56 |
+
rss_url = "https://finance.yahoo.com/news/rssindex"
|
| 57 |
+
feed = feedparser.parse(rss_url)
|
| 58 |
+
|
| 59 |
+
for entry in feed.entries[:max_articles]:
|
| 60 |
+
news_list.append({
|
| 61 |
+
'title': entry.get('title', 'No title'),
|
| 62 |
+
'link': entry.get('link', ''),
|
| 63 |
+
'summary': entry.get('summary', '')[:300], # จำกัด 300 ตัวอักษร
|
| 64 |
+
'published': entry.get('published', 'N/A')
|
| 65 |
+
})
|
| 66 |
+
|
| 67 |
+
# ถ้าไม่มีข่าว ลองวิธีสำรอง
|
| 68 |
+
if not news_list:
|
| 69 |
+
news_list = self._get_fallback_news(max_articles)
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"Error scraping news: {e}")
|
| 73 |
+
news_list = self._get_fallback_news(max_articles)
|
| 74 |
+
|
| 75 |
+
return news_list
|
| 76 |
+
|
| 77 |
+
def _get_fallback_news(self, max_articles):
|
| 78 |
+
"""วิธีสำรองในกรณีที่ดึงข่าวไม่ได้"""
|
| 79 |
+
try:
|
| 80 |
+
# ใช้ Yahoo Finance RSS feed แบบง่าย
|
| 81 |
+
url = "https://finance.yahoo.com/rss/topstories"
|
| 82 |
+
feed = feedparser.parse(url)
|
| 83 |
+
|
| 84 |
+
news_list = []
|
| 85 |
+
for entry in feed.entries[:max_articles]:
|
| 86 |
+
news_list.append({
|
| 87 |
+
'title': entry.get('title', 'No title'),
|
| 88 |
+
'link': entry.get('link', ''),
|
| 89 |
+
'summary': entry.get('summary', '')[:300],
|
| 90 |
+
'published': entry.get('published', 'N/A')
|
| 91 |
+
})
|
| 92 |
+
|
| 93 |
+
return news_list
|
| 94 |
+
except:
|
| 95 |
+
return [{
|
| 96 |
+
'title': 'Unable to fetch news',
|
| 97 |
+
'link': '',
|
| 98 |
+
'summary': 'Please try again later',
|
| 99 |
+
'published': 'N/A'
|
| 100 |
+
}]
|
| 101 |
+
|
| 102 |
+
def search_news(self, keyword, max_articles=10):
|
| 103 |
+
"""
|
| 104 |
+
ค้นหาข่าวด้วย keyword
|
| 105 |
+
|
| 106 |
+
Args:
|
| 107 |
+
keyword: คำค้นหา
|
| 108 |
+
max_articles: จำนวนข่าว
|
| 109 |
+
|
| 110 |
+
Returns:
|
| 111 |
+
list: รายการข่าวที่เกี่ยวข้อง
|
| 112 |
+
"""
|
| 113 |
+
try:
|
| 114 |
+
search_url = f"{self.base_url}/search?p={keyword}"
|
| 115 |
+
response = requests.get(search_url, headers=self.headers, timeout=10)
|
| 116 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 117 |
+
|
| 118 |
+
news_list = []
|
| 119 |
+
articles = soup.find_all('div', class_='Ov(h)', limit=max_articles)
|
| 120 |
+
|
| 121 |
+
for article in articles:
|
| 122 |
+
try:
|
| 123 |
+
title = article.find('h3')
|
| 124 |
+
link = article.find('a')
|
| 125 |
+
|
| 126 |
+
if title and link:
|
| 127 |
+
news_list.append({
|
| 128 |
+
'title': title.get_text(strip=True),
|
| 129 |
+
'link': link.get('href', ''),
|
| 130 |
+
'summary': '',
|
| 131 |
+
'published': 'Recent'
|
| 132 |
+
})
|
| 133 |
+
except:
|
| 134 |
+
continue
|
| 135 |
+
|
| 136 |
+
return news_list if news_list else self.get_latest_news(max_articles=max_articles)
|
| 137 |
+
|
| 138 |
+
except Exception as e:
|
| 139 |
+
print(f"Search error: {e}")
|
| 140 |
+
return self.get_latest_news(max_articles=max_articles)
|