0

So i am scrapping the nse india results calendar site via using curl command and even after that its giving me "Resource not Found" error . Here's my code

url = f"https://www.nseindia.com/api/event-calendar?index=equities"
header1 ="Host:www.nseindia.com"
header2 = "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0"
header3 ="Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
header4 ="Accept-Language:en-US,en;q=0.5"
header5 ="Accept-Encoding:gzip, deflate, br"
header6 ="DNT:1"
header7 ="Connection:keep-alive"
header8 ="Upgrade-Insecure-Requests:1"
header9 ="Pragma:no-cache"
header10 ="Cache-Control:no-cache"

    def run_curl_command(curl_command, max_attempts):
        result = os.popen(curl_command).read()
        count = 0
        while "Resource not found" in result and  count < max_attempts:
            result = os.popen(curl_command).read()
            count += 1
            time.sleep(1)
        print("API Read")
        result = json.loads(result)
        result = pd.DataFrame(result)

def init():
    max_attempts = 100
    curl_command = f'curl "{url}" -H "{header1}" -H "{header2}" -H "{header3}" -H "{header4}" -H "{header5}" -H "{header6}" -H "{header7}" -H "{header8}" -H "{header9}" -H "{header10}" --compressed '
    print(f"curl_command : {curl_command}")
    run_curl_command(curl_command, max_attempts)

init()
Artem Sokolov
  • 13,196
  • 4
  • 43
  • 74

1 Answers1

0

Use the nsefetch() function as documented here https://unofficed.com/nse-python/documentation/nsefetch/

In case you want python-requests method

from nsepython import *
positions = nsefetch('https://www.nseindia.com/api/event-calendar?index=equities')
print(positions )

In case you want curl method

from nsepythonserver import *
positions = nsefetch('https://www.nseindia.com/api/event-calendar?index=equities')
print(positions )

NSE checked if you are directly accessing the link or you are coming there after visiting the main page. That needs to be mimicked using cookies.

Amit Ghosh
  • 1,500
  • 13
  • 18