Akshayram1 commited on
Commit
afc2587
·
verified ·
1 Parent(s): b7c7c89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -23
app.py CHANGED
@@ -6,6 +6,7 @@ import plotly
6
  import plotly.express as px
7
  import json # for graph plotting in website
8
  # NLTK VADER for sentiment analysis
 
9
  import nltk
10
  nltk.downloader.download('vader_lexicon')
11
  from nltk.sentiment.vader import SentimentIntensityAnalyzer
@@ -29,40 +30,41 @@ def get_news(ticker):
29
  return news_table
30
 
31
  # parse news into dataframe
 
 
32
  def parse_news(news_table):
33
  parsed_news = []
34
- today_string = datetime.datetime.today().strftime('%Y-%m-%d')
35
 
36
  for x in news_table.findAll('tr'):
37
  try:
38
- # read the text from each tr tag into text
39
- # get text from a only
40
- text = x.a.get_text()
41
- # splite text in the td tag into a list
42
- date_scrape = x.td.text.split()
43
- # if the length of 'date_scrape' is 1, load 'time' as the only element
44
-
45
  if len(date_scrape) == 1:
 
46
  time = date_scrape[0]
47
-
48
- # else load 'date' as the 1st element and 'time' as the second
49
  else:
50
  date = date_scrape[0]
51
  time = date_scrape[1]
 
 
 
 
 
 
 
52
 
53
- # Append ticker, date, time and headline as a list to the 'parsed_news' list
54
- parsed_news.append([date, time, text])
55
- except:
56
- pass
57
-
58
- # Set column names
59
- columns = ['date', 'time', 'headline']
60
- # Convert the parsed_news list into a DataFrame called 'parsed_and_scored_news'
61
- parsed_news_df = pd.DataFrame(parsed_news, columns=columns)
62
- # Create a pandas datetime object from the strings in 'date' and 'time' column
63
- parsed_news_df['date'] = parsed_news_df['date'].replace("Today", today_string)
64
- parsed_news_df['datetime'] = pd.to_datetime(parsed_news_df['date'] + ' ' + parsed_news_df['time'])
65
-
66
  return parsed_news_df
67
 
68
 
 
6
  import plotly.express as px
7
  import json # for graph plotting in website
8
  # NLTK VADER for sentiment analysis
9
+ from dateutil import parser
10
  import nltk
11
  nltk.downloader.download('vader_lexicon')
12
  from nltk.sentiment.vader import SentimentIntensityAnalyzer
 
30
  return news_table
31
 
32
  # parse news into dataframe
33
+
34
+
35
  def parse_news(news_table):
36
  parsed_news = []
 
37
 
38
  for x in news_table.findAll('tr'):
39
  try:
40
+ # Get the headline text
41
+ text = x.a.get_text()
42
+ # Get the date and time from the first <td> tag
43
+ date_scrape = x.td.text.strip().split()
44
+
45
+ # Handle cases where only time is present
 
46
  if len(date_scrape) == 1:
47
+ date = datetime.datetime.today().strftime('%Y-%m-%d')
48
  time = date_scrape[0]
 
 
49
  else:
50
  date = date_scrape[0]
51
  time = date_scrape[1]
52
+
53
+ # Parse the date and time using dateutil.parser
54
+ datetime_str = f"{date} {time}"
55
+ datetime_parsed = parser.parse(datetime_str)
56
+
57
+ # Append the parsed news to the list
58
+ parsed_news.append([datetime_parsed, text])
59
 
60
+ except Exception as e:
61
+ print("Error parsing news:", e)
62
+ continue
63
+
64
+ # Convert the list to a DataFrame
65
+ columns = ['datetime', 'headline']
66
+ parsed_news_df = pd.DataFrame(parsed_news, columns=columns)
67
+
 
 
 
 
 
68
  return parsed_news_df
69
 
70