-
Notifications
You must be signed in to change notification settings - Fork 1
/
MorningStar_wrapper.py
77 lines (58 loc) · 2.38 KB
/
MorningStar_wrapper.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
import json
import requests
from bs4 import BeautifulSoup
class Wrapper:
"""
Wrapper to return a ticker, based on ISIN input.
Data from Morningstar.com
"""
# If True prints some basic feedback from the Wrapper
debug = False
def __init__(self, ISIN=''):
self.ISIN = ISIN.upper()
if self.ISIN:
# Opens search URL and returns a Requests-object
r = self.startConnection()
# Creates soup and finds the correct result data based on a request-object
self.makeSoup(r)
def setISIN(self, ISIN):
""" Sets the value of ISIN, it's required to set this, either in the __init__ or with this function """
self.ISIN = ISIN.upper()
def startConnection(self):
""" Stars the connection to the search URL and returns a request-object if succeeded """
PREFIX = 'http://www.morningstar.com/search.html?q='
search_url = f'{PREFIX}+{self.ISIN}'
if self.debug:
print(search_url)
r = requests.get(search_url)
if r.status_code == 200:
return r
else:
return None
def makeSoup(self, request):
""" Creates a BS object and filters the data we need """
soup = BeautifulSoup(request.text, 'html.parser')
for div in soup.find_all('div'):
# Limit our search to the search results
if div.get('class') == ['search-list-content']:
# Check if we found correct result, containing our ISIN
if div.get('data-key').strip() == self.ISIN:
self.data = div.get('data-initialdata')
break
def getTicker(self):
""" Returns a ticker based on the filtered data, which is collected in makeSoup() """
if self.debug:
print('DATA: ' + self.data)
myJSON = json.loads(self.data)
if self.debug:
print('JSON: ' + str(myJSON))
print('Result part: ' + str(myJSON['result']))
new_dict = myJSON['m']
if self.debug:
print('NEW DICT: ', end='')
print(list(new_dict))
# The ticker is part of a large string, followed by the OS001-tag.
pos_tag = str(new_dict).find('OS001')
pos_comma = str(new_dict)[pos_tag:].find(',')
ticker = str(new_dict)[pos_tag + 9:pos_tag + pos_comma - 1]
return ticker