nick5363 commited on
Commit
09d58c7
·
verified ·
1 Parent(s): aa0b587

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -19
app.py CHANGED
@@ -4,33 +4,49 @@ import gradio as gr
4
 
5
  def get_latest_yahoo_news():
6
  url = "https://finance.yahoo.com/topic/latest-news/"
7
- headers = {"User-Agent": "Mozilla/5.0"}
 
 
8
 
9
  try:
10
- response = requests.get(url, headers=headers)
 
 
11
  soup = BeautifulSoup(response.text, "html.parser")
12
 
13
- articles = soup.select("li.js-stream-content a.js-content-viewer")[:5] # lấy 5 tin đầu
14
- except Exception as e:
15
- return f"Lỗi khi tải tin tức: {e}"
 
 
16
 
17
- if not articles:
18
- return "Không tìm thấy tin tức nào từ Yahoo Finance."
 
 
 
 
 
 
 
 
19
 
20
- result = "# Tin tức Mới nhất từ Yahoo Finance\n\n"
21
- for i, article in enumerate(articles):
22
- title = article.get_text(strip=True)
23
- link = article.get("href")
24
- if link and not link.startswith("http"):
25
- link = "https://finance.yahoo.com" + link
26
- result += f"**{i+1}. [{title}]({link})**\n\n"
27
 
28
- return result
 
 
 
29
 
30
- gr.Interface(
 
31
  fn=get_latest_yahoo_news,
32
  inputs=None,
33
  outputs="markdown",
34
- title="Tin Tức Mới Nhất (Yahoo Finance)",
35
- 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ì."
36
- ).launch()
 
 
 
 
 
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
+ # Thêm timeout để tránh treo request
13
+ response = requests.get(url, headers=headers, timeout=10)
14
+ response.raise_for_status() # Kiểm tra lỗi HTTP
15
  soup = BeautifulSoup(response.text, "html.parser")
16
 
17
+ # Cập nhật selector dựa trên cấu trúc mới của Yahoo Finance
18
+ articles = soup.select("li.js-stream-content h3 a")[:5] # Selector mới, lấy tiêu đề từ h3
19
+
20
+ if not articles:
21
+ 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."
22
 
23
+ result = "# Tin tức Mới nhất từ Yahoo Finance\n\n"
24
+ for i, article in enumerate(articles):
25
+ title = article.get_text(strip=True)
26
+ link = article.get("href")
27
+ if link:
28
+ # Đảm bảo link đầy đủ
29
+ link = link if link.startswith("http") else f"https://finance.yahoo.com{link}"
30
+ result += f"**{i+1}. [{title}]({link})**\n\n"
31
+ else:
32
+ result += f"**{i+1}. {title}** (Không có link)\n\n"
33
 
34
+ return result
 
 
 
 
 
 
35
 
36
+ except requests.exceptions.RequestException as e:
37
+ return f"Lỗi khi tải tin tức: {e}"
38
+ except Exception as e:
39
+ return f"Lỗi không xác định: {e}"
40
 
41
+ # Cấu hình Gradio Interface
42
+ interface = gr.Interface(
43
  fn=get_latest_yahoo_news,
44
  inputs=None,
45
  outputs="markdown",
46
+ title "Tin Tức Mới Nhất (Yahoo Finance)",
47
+ 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ì.",
48
+ allow_flagging="never" # Tắt flagging để đơn giản hóa
49
+ )
50
+
51
+ if __name__ == "__main__":
52
+ interface.launch(server_name="0.0.0.0", server_port=7860)