nick5363 commited on
Commit
1b9f5de
·
verified ·
1 Parent(s): 7212ffb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -41
app.py CHANGED
@@ -1,49 +1,26 @@
1
- import requests
2
- from bs4 import BeautifulSoup
3
  import gradio as gr
4
 
5
- def get_latest_yahoo_news():
6
- url = "https://finance.yahoo.com/topic/latest-news/"
7
- headers = {
8
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
9
- }
10
 
11
- try:
12
- response = requests.get(url, headers=headers, timeout=10)
13
- response.raise_for_status()
14
- soup = BeautifulSoup(response.text, "html.parser")
15
 
16
- articles = soup.select("li.js-stream-content h3 a")[:5]
 
 
 
 
 
17
 
18
- if not articles:
19
- return "Không tìm thấy tin tức nào từ Yahoo Finance. Có thể trang web đã thay đổi cấu trúc."
20
 
21
- result = "# Tin tức Mới nhất từ Yahoo Finance\n\n"
22
- for i, article in enumerate(articles):
23
- title = article.get_text(strip=True)
24
- link = article.get("href")
25
- if link:
26
- link = link if link.startswith("http") else f"https://finance.yahoo.com{link}"
27
- result += f"**{i+1}. [{title}]({link})**\n\n"
28
- else:
29
- result += f"**{i+1}. {title}** (Không có link)\n\n"
30
-
31
- return result
32
-
33
- except requests.exceptions.RequestException as e:
34
- return f"Lỗi khi tải tin tức: {e}"
35
- except Exception as e:
36
- return f"Lỗi không xác định: {e}"
37
-
38
- # Sửa lỗi cú pháp: thêm dấu = cho title
39
- interface = gr.Interface(
40
- fn=get_latest_yahoo_news,
41
  inputs=None,
42
  outputs="markdown",
43
- title="Tin Tức Mới Nhất (Yahoo Finance)", # Sửa ở đây
44
- description="Hiển thị tự động 5 tin tức tài chính/kỹ thuật/macro mới nhất từ Yahoo – không cần nhập gì.",
45
- allow_flagging="never"
46
- )
47
-
48
- if __name__ == "__main__":
49
- interface.launch(server_name="0.0.0.0", server_port=7860)
 
1
+ import feedparser
 
2
  import gradio as gr
3
 
4
+ def get_latest_rss_news():
5
+ feed_url = "https://finance.yahoo.com/news/rssindex"
6
+ feed = feedparser.parse(feed_url)
 
 
7
 
8
+ if not feed.entries:
9
+ return "Không tìm thấy tin tức nào từ Yahoo RSS."
 
 
10
 
11
+ result = "# Tin tức Mới nhất từ Yahoo Finance (RSS)\n\n"
12
+ for i, entry in enumerate(feed.entries[:5]):
13
+ title = entry.title
14
+ link = entry.link
15
+ published = entry.published
16
+ result += f"**{i+1}. [{title}]({link})** \n<sub>{published}</sub>\n\n"
17
 
18
+ return result
 
19
 
20
+ gr.Interface(
21
+ fn=get_latest_rss_news,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  inputs=None,
23
  outputs="markdown",
24
+ title="Tin tức Mới nhất từ Yahoo (RSS)",
25
+ description="Tự động hiển thị 5 tin tài chính/kỹ thuật/macro mới nhất từ Yahoo Finance – không cần nhập gì."
26
+ ).launch()