Reputation: 9
import requests
import json
url='https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY'
headers= {'user-agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36',
"accept-language":"en-US,en;q=0.9","accept-encoding":"gzip, deflate"}
def fetch_oi():
r=requests.get(url,headers=headers).json()
print(r)
with open("oidata.json","w") as files:
files.write(json.dumps(r,indent=4, sort_keys=True))
def main():
fetch_oi()
if __name__=='__main__':
main()
** in this code i'm getting error in r=requests.get(url,headers=headers).json() ^ IndentationError: expected an indented block
what i'm doing wronge? plzz help
Upvotes: 0
Views: 648
Reputation: 160
Issue is in the body of
def fetch_oi():
Put space to the body
My suggestions is to use sublime IDE. So, when you select the content you can visible the content that you have used space or tab. Also using tab is the good approach rather than space
I have seen that you have used space. So adjusting and using space will make you troublesome at long run.
Upvotes: 0
Reputation: 2909
The body of your function fetch_oi
is unindented.
import requests
import json
url='https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY'
headers= {'user-agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36', "accept-language":"en-US,en;q=0.9","accept-encoding":"gzip, deflate"}
def fetch_oi():
r=requests.get(url,headers=headers).json()
print(r)
with open("oidata.json","w") as files:
files.write(json.dumps(r,indent=4, sort_keys=True))
def main():
fetch_oi()
if __name__=='__main__':
main()
Upvotes: 1
Reputation: 651
I believe it is clear that you need
def fetch_oi():
r=requests.get(url,headers=headers).json()
print(r)
with open("oidata.json","w") as files:
files.write(json.dumps(r,indent=4, sort_keys=True))
instead of what you have (that is one tab more in fetch_io). In python you have to be careful with tabs. You need one identation level after each function. Take a look here: https://docs.python.org/2.0/ref/indentation.html
Upvotes: 2
Reputation: 965
The code in a function definition needs to be indented.
import json
url='https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY'
headers= {'user-agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36',
"accept-language":"en-US,en;q=0.9","accept-encoding":"gzip, deflate"}
def fetch_oi():
r=requests.get(url,headers=headers).json()
print(r)
with open("oidata.json","w") as files:
files.write(json.dumps(r,indent=4, sort_keys=True))
def main():
fetch_oi()
if __name__=='__main__':
main()
Python depends heavily on correct indentation, see: https://www.w3schools.in/python-tutorial/concept-of-indentation-in-python/
Upvotes: 3
Reputation: 462
After your def fetch_oi():
you are supposed to indent the line r=requests.get(url,headers=headers).json()
since it is a function
def fetch_oi():
r=requests.get(url,headers=headers).json()
print(r)
then depending on where your function ends, you should keep indenting
Upvotes: 2