-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentiment_analysis_on_stocks.py
61 lines (37 loc) · 1.4 KB
/
sentiment_analysis_on_stocks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from urllib.request import urlopen, Request
from bs4 import BeautifulSoup
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import pandas as pd
import matplotlib.pyplot as plt
finviz_url = 'https://finviz.com/quote.ashx?t='
tickers=['AMZN' , 'AMD' , 'FB' , 'GS' , 'JPM']
news_tables={}
for ticker in tickers:
url= finviz_url +ticker
req= Request(url=url,headers={'user-agent':'my-app'})
response = urlopen(req)
html = BeautifulSoup(response, 'html')
news_table=html.find(id='news-table')
news_tables[ticker]=news_table
parsed_data=[]
for ticker,news_table in news_tables.items():
for row in news_table.find_all('tr'):
title=row.a.get_text()
date_data=row.td.text.split(' ')
if len(date_data)==1:
time=date_data[0]
else:
date=date_data[0]
time=date_data[1]
parsed_data.append([ticker,date,time,title])
df = pd.DataFrame (parsed_data, columns=['ticker','date','time','title'])
vader=SentimentIntensityAnalyzer()
f=lambda title:vader.polarity_scores(title)['compound']
df['compound'] = df['title'].apply(f)
df['date']=pd.to_datetime(df.date).dt.date
plt.figure(figsize=(10,8))
mean_df=df.groupby(['ticker','date']).mean()
mean_df=mean_df.unstack()
mean_df=mean_df.xs('compound',axis="columns").transpose()
mean_df.plot(kind='bar')
plt.show()