[Python] Why my script's working is inconsistent(sometimes output sometimes not)

Discussão em 'Python' iniciado por Stack, Março 29, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to fetch data from NSE India. This is my script how I tried but it's working is not consistent since few days. Earlier it was working well.Plz can anyone give me the idea how to fix this....

    import requests
    import pandas as pd
    import time
    import json

    def create_session():
    base_url = "https://www.nseindia.com"
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36', 'Accept': 'application/json, text/plain, */*', 'Accept-Language': 'en-US,en;q=0.5', 'Connection': 'keep-alive'}
    session = requests.Session()
    session.get(base_url, headers=headers, timeout=(60, 60))
    return session, headers

    def spurts_data(session, headers):
    url = "https://www.nseindia.com/api/live-analysis-oi-spurts-underlyings"

    try:
    res = session.get(url, headers=headers, timeout=10)
    res_data = res.json()
    df_sp = pd.DataFrame(res_data['data'])
    df_sp.columns = ['SYMBOL', 'LTSTOI', 'PREVOI', 'CHOI', '%CHOI', 'VOLUME', 'futValue', 'optValue', 'total', 'premValue', 'UDRLNG']
    df_sp = df_sp[['SYMBOL', 'LTSTOI', 'CHOI', 'VOLUME', 'UDRLNG', '%CHOI']]

    remove = ('BANKNIFTY', 'FINNIFTY', 'MIDCPNIFTY', 'NIFTY', '-', 'NIFTYNXT50')
    df_sp = df_sp[~df_sp["SYMBOL"].isin(remove)].reset_index(drop=True)

    return df_sp
    except requests.exceptions.RequestException as e:
    print(f"Error fetching spurts data: {e}")
    return pd.DataFrame()

    # Create session
    session, headers = create_session()

    # Get spurts data
    df_sp = spurts_data(session, headers)

    print(df_sp)

    Continue reading...

Compartilhe esta Página