Spaces:
Build error
Build error
Update app.py
Browse files
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 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
#
|
| 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 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 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 |
|