Shubhanker Tiwari
Shubhanker Tiwari

Reputation: 1

Data not able to read from the link with python request

I am trying to read data from this link: https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY using python request and urllib library. I tried both libraries but not able to see even the status code of url. Please suggest what is wrong with this. I am attaching my code as well please look for it. and tell me where i am doing wrong

import csv
import requests
from csv import reader
import xlrd
import pandas
import urllib.request
from bs4 import BeautifulSoup


# open a connection to a URL using urllib
webUrl  = urllib.request.urlopen('https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY')

#get the result code and print it
print ("result code: " + str(webUrl.getcode()))

read the data from the URL and print it
data = webUrl.read()
print (data)

Upvotes: 0

Views: 88

Answers (1)

Adam
Adam

Reputation: 1309

You need to add headers

I made this just to test it out

import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'My User Agent 1.0',
    'From': '[email protected]'  # This is another valid field
}

# open a connection to a URL using urllib
webUrl  = requests.get('https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY', headers=headers).text


#read the data from the URL and print it
soup = BeautifulSoup(webUrl, 'html.parser')
print (soup.prettify())

Upvotes: 1

Related Questions