algoTrader
algoTrader

Reputation: 23

Why request.get() is giving me an error in python?

import requests
url="https://beta.nseindia.com/api/option-chain-indices?symbol=NIFTY"
requests.get(url)

I am using requests but it is giving me a timeout error..but when i open same link in browser its working fine

Upvotes: 0

Views: 2434

Answers (2)

Anteino
Anteino

Reputation: 1136

If you scroll all the way up through your errors you will see an InsecureRequestWarning, meaning you were making an unverified HTTPS request. This can be resolved by following this guide.

First, you need to install some packages to handle the certificates:

pip install certifi pip urllib3[secure]

Then, in your code you create a PoolManager that verifies certificates when making requests:

import requests
import certifi
import urllib3

http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())
url = 'https://beta.nseindia.com/api/option-chain-indices?symbol=NIFTY'

http.request('GET', url, verify=False)

Depending on which version of python you are using this might or might not work.

Upvotes: 2

Hussain Bohra
Hussain Bohra

Reputation: 1005

Try with some user agent string header:

import requests
url="https://beta.nseindia.com/api/option-chain-indices?symbol=NIFTY"
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get(url, headers=headers)


>>> response
<Response [200]>
>>>

beta.nseindia.com/ has a policy which blocks the request coming from bot.

Upvotes: 4

Related Questions