-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasx_data.py
78 lines (53 loc) · 1.96 KB
/
asx_data.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
78
from bs4 import BeautifulSoup
import requests
import ast
def last_price(code):
address = 'https://www.asx.com.au/asx/1/share/' + code + '/'
source = requests.get(address).text
soup = BeautifulSoup(source, 'lxml')
data = soup.p.text
last_price_loc = data.find('last_price')
start = data.find(':', last_price_loc) + 1
end = data.find(',', start)
if last_price_loc == -1:
return 0
return float(data[start:end])
def basic(code):
address = 'https://www.asx.com.au/asx/1/share/' + code + '/'
source = requests.get(address).text
soup = BeautifulSoup(source, 'lxml')
data = soup.p.text
last_price_loc = data.find('last_price')
if last_price_loc == -1:
return float('nan'), float('nan')
start = data.find(':', last_price_loc) + 1
end = data.find(',', start)
last_price = float(data[start:end])
volume_loc = data.find('volume')
start = data.find(':', volume_loc) + 1
end = data.find(',', start)
volume = int(data[start:end])
return last_price, volume
def basic_d(code):
address = 'https://www.asx.com.au/asx/1/share/' + code + '/'
source = requests.get(address).text
soup = BeautifulSoup(source, 'lxml')
data = soup.p.text
last_price_loc = data.find('last_price')
if last_price_loc == -1:
return code, float('nan'), float('nan')
start = data.find(':', last_price_loc) + 1
end = data.find(',', start)
last_price = float(data[start:end])
volume_loc = data.find('volume')
start = data.find(':', volume_loc) + 1
end = data.find(',', start)
volume = int(data[start:end])
return code, last_price, volume
def get_eod(code, no):
address = 'https://www.asx.com.au/asx/1/share/' + code + '/prices?interval=daily&count=' + str(no)
source = requests.get(address).text
soup = BeautifulSoup(source, 'lxml')
data = soup.p.text
output = ast.literal_eval(data.replace('{"data":', '')[:-1])
return output