Szeyu commited on
Commit
e3ea312
·
verified ·
1 Parent(s): 47ffeb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -4
app.py CHANGED
@@ -38,15 +38,36 @@ sentiment_pipeline = pipeline("sentiment-analysis", model=model, tokenizer=token
38
  def fetch_news(ticker):
39
  try:
40
  url = f"https://finviz.com/quote.ashx?t={ticker}"
41
- headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
 
 
 
 
 
 
42
  response = requests.get(url, headers=headers)
 
 
 
 
43
  soup = BeautifulSoup(response.text, 'html.parser')
 
 
 
 
 
44
  news_table = soup.find(id='news-table')
 
 
 
 
45
  news = []
46
  for row in news_table.findAll('tr')[:50]: # Fetch up to 50 articles
47
- title = row.a.get_text()
48
- link = row.a['href']
49
- news.append({'title': title, 'link': link})
 
 
50
  return news
51
  except Exception as e:
52
  st.error(f"Failed to fetch news for {ticker}: {e}")
@@ -66,6 +87,8 @@ st.markdown("""
66
  This tool parses stock tickers and analyzes the sentiment of related news articles.
67
 
68
  💡 *Example input:* `META, NVDA, AAPL, NTES, NCTY`
 
 
69
  """)
70
 
71
  # Input field for stock tickers
 
38
  def fetch_news(ticker):
39
  try:
40
  url = f"https://finviz.com/quote.ashx?t={ticker}"
41
+ headers = {
42
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
43
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
44
+ 'Accept-Language': 'en-US,en;q=0.5',
45
+ 'Referer': 'https://finviz.com/',
46
+ 'Connection': 'keep-alive',
47
+ }
48
  response = requests.get(url, headers=headers)
49
+ if response.status_code != 200:
50
+ st.error(f"Failed to fetch page for {ticker}: Status code {response.status_code}")
51
+ return []
52
+
53
  soup = BeautifulSoup(response.text, 'html.parser')
54
+ title = soup.title.text if soup.title else ""
55
+ if ticker not in title:
56
+ st.error(f"Page for {ticker} not found or access denied.")
57
+ return []
58
+
59
  news_table = soup.find(id='news-table')
60
+ if news_table is None:
61
+ st.error(f"News table not found for {ticker}. The website structure might have changed.")
62
+ return []
63
+
64
  news = []
65
  for row in news_table.findAll('tr')[:50]: # Fetch up to 50 articles
66
+ a_tag = row.find('a')
67
+ if a_tag:
68
+ title = a_tag.get_text()
69
+ link = a_tag['href']
70
+ news.append({'title': title, 'link': link})
71
  return news
72
  except Exception as e:
73
  st.error(f"Failed to fetch news for {ticker}: {e}")
 
87
  This tool parses stock tickers and analyzes the sentiment of related news articles.
88
 
89
  💡 *Example input:* `META, NVDA, AAPL, NTES, NCTY`
90
+
91
+ **Note:** If news fetching fails, it might be due to changes in the Finviz website structure or access restrictions. Please verify the website manually or try again later.
92
  """)
93
 
94
  # Input field for stock tickers