forked from Dewansh1029/web-scrption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.py
31 lines (26 loc) · 1.28 KB
/
web.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
import requests
import json
# Fetch the data from any url with some expiration time
def fetch_oi(url,expiry):
r = requests.get(url).json()
#print(r)
'''if expiry:
ce_values = [data['CE'] for data in r['records']['data'] if "CE" in data and str(data['expiryData']).lower()==str(expiry).lower()]
pe_values = [data['PE'] for data in r['records']['data'] if "PE" in data and str(data['expiryData']).lower()==str(expiry).lower()]
else:
ce_values = [data['CE'] for data in r['filtered']['data'] if "CE" in data]
pe_values = [data['PE'] for data in r['filtered']['data'] if "PE" in data]
ce_data =pd.DataFrame(ce_values)
pe_data =pd.DataFrame(pe_values)'''
with open("old.json","w") as files:
files.write(json.dumps(r,indent=4,sort_key = True))
ce_value = [data['CE'] for data in r['filtered']['data'] if "CE" in data]
pe_value = [data['PE'] for data in r['filtered']['data'] if "PE" in data]
print(ce_value)
print(pe_value)
def main():
url="https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY"
expiry ="24-sep-2020"
fetch_oi(url,expiry)
if __name__=='__main__':
main()