Python Web Scraping

 import requests

import json

cookies = None;
url = 'https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, '
                         'like Gecko) '
                         'Chrome/80.0.3987.149 Safari/537.36',
           'upgrade-insecure-requests': '0',
           'accept-language': 'en,gu;q=0.9,hi;q=0.8', 'accept-encoding': 'gzip, deflate, br'}

def loadnsecookie():
    baseurl = "https://www.nseindia.com/"
    session = requests.Session()
    request = session.get(baseurl, headers=headers, timeout=5)
    cookies = dict(request.cookies)


#response = session.get(url, headers=headers, timeout=5, cookies=cookies)
#print(response.json())
#r = requests.get(url, headers=headers)
expiry = "20-Oct-2022"
#print(expiry)
#exit

def fetch_oi():
    print('fetch oi called')
    r = requests.get(url, headers=headers, timeout=5, cookies=cookies).json()
    print('response received')
    #with open('oidata.json',"w") as files:
    #    files.write(json.dumps(r, indent=4, sort_keys=True))
    #print('file written')


def main():
    loadnsecookie()
    fetch_oi()

print(__name__)
if __name__ == '__main__':
    print(1)
    main()
    print(2)

Search This Blog

Link Within Related Posts Plugin for WordPress, Blogger...