-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
77 lines (44 loc) · 1.62 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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=['INTC','MSFT','AMD','HPE','DELL']
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
"""intel_data=news_tables['INTC']
intel_rows=intel_data.findAll('tr')
for index,row in enumerate(intel_rows):
title=row.a.text
timestamp=row.td.text
print(timestamp + " " + title)
"""
parsed_data = []
for ticker, news_table in news_tables.items():
for row in news_table.findAll('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()
func = lambda title: vader.polarity_scores(title)['compound']
df["Compound"]=df["title"].apply(func)
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()