forked from abhiin1947/stockscrape
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFutures.py
67 lines (62 loc) · 2.79 KB
/
Futures.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
import urllib2
import json
def get_data(url):
req = urllib2.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Ubuntu Chromium/24.0.1312.56 Chrome/24.0.1312.56 Safari/537.17')
req.add_header('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
resp = urllib2.urlopen(req);
#print resp.read()
return resp.read()
def parse_data_all(url):
html = get_data(url)
data2 = html.split("-;-")
if len(data2) > 1:
data = data2[1]
if len(data) > 1:
contracts = data.split("~")
data_arr = [];
for element in contracts:
got_ba_price = 0;
got_bb_price = 0;
e = {}
if element.split(":")[0] != "-":
e['contract'] = element.split(":")[0]
if element.split(":")[1] != "-":
e['type'] = element.split(":")[1]
if element.split(":")[2] != "-":
e['conversion'] = element.split(":")[2]
if element.split(":")[3] != "-":
e['timestamp'] = element.split(":")[3]
if element.split(":")[6] != "-":
e['bb_qty'] = element.split(":")[6]
if element.split(":")[7] != "-":
e['bb_price'] = element.split(":")[7]
got_bb_price = 1
if element.split(":")[8] != "-":
e['ba_price'] = element.split(":")[8]
got_ba_price = 1
if element.split(":")[9] != "-":
e['ba_qty'] = element.split(":")[9]
if got_ba_price == 1 and got_bb_price == 1:
e['spread'] = "{0:.4f}".format(float(e['ba_price'])-float(e['bb_price']))
if element.split(":")[10] != "-":
e['ltp'] = element.split(":")[10]
if element.split(":")[11] != "-":
e['volume'] = element.split(":")[11].replace(",", "")
if element.split(":")[12] != "-":
e['oi'] = element.split(":")[12].replace(",", "")
if element.split(":")[13] != "-":
e['value'] = element.split(":")[13].replace(",", "")
if element.split(":")[14] != "-":
e['notrades'] = element.split(":")[14].replace(",", "")
data_arr.append(e)
return json.dumps(data_arr)
else:
return ""
def get_futures_data(option):
if option is not None:
return parse_data_all('http://www.nseindia.com/marketinfo/fxTracker/priceWatchData.jsp?instrument=FUTCUR¤cy='+option)
else:
return None
if __name__=="__main__":
print get_futures_data("USDINR")