PJamila commited on
Commit
4bc3824
1 Parent(s): e6bea59

Create fetch_news.py

Browse files
Files changed (1) hide show
  1. fetch_news.py +24 -0
fetch_news.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ib_insync import *
2
+
3
+ ib = IB()
4
+ ib.connect('127.0.0.1', 7496, clientId=1)
5
+
6
+ news_providers = ib.reqNewsProviders()
7
+
8
+ codes = '+'.join(news_provider.code for news_provider in news_providers)
9
+
10
+ symbols = ['MSFT', 'NVDA', 'GOOG', 'META', 'AAPL', 'TSM']
11
+
12
+ for symbol in symbols:
13
+ stock = Stock(symbol, 'SMART', 'USD')
14
+ ib.qualifyContracts(stock)
15
+ headlines = ib.reqHistoricalNews(stock.conId, codes, '', '', 100)
16
+
17
+ for headline in headlines:
18
+ article_date = headline.time.date()
19
+ article = ib.reqNewsArticle(headline.providerCode, headline.articleId)
20
+
21
+ article_filename = f"articles/{article_date}-{symbol}-{headline.articleId}.html"
22
+
23
+ with open(article_filename, 'w') as f:
24
+ f.write(article.articleText)