I am trying to download a CSV file with Python 3.x The path of the file is: https://www.nseindia.com/content/fo/fo_mktlots.csv
I have found three ways to do it. Of the three only one method works. I wanted to know why or what I am doing wrong.
Method 1: (Unsuccessful)
import pandas as pd mytable = pd.read_table("https://www.nseindia.com/content/fo/fo_mktlots.csv",sep=",") print(mytable)
But I am getting the following error :
- HTTPError: HTTP Error 403: Forbidden
Method 2: (Unsuccessful)
from urllib.request import Request, urlopen url='https://www.nseindia.com/content/fo/fo_mktlots.csv' url_request = Request(url, headers={'User-Agent': 'Mozilla/5.0'}) html = urlopen(url_request ).read()
Got the same error as before :
- HTTPError: HTTP Error 403: Forbidden
Method 3: (Successful)
import requests import pandas as pd url = 'https://www.nseindia.com/content/fo/fo_mktlots.csv' r = requests.get(url) df = pd.read_csv(StringIO(r.text))
I am also able to open the file with Excel VBA as below:
Workbooks.Open Filename:="https://www.nseindia.com/content/fo/fo_mktlots.csv"
Also, is there any other method to do the same?