The data that is being acquired by this code, is not being formatted into proper csv format.
import requests
import csv
def Download_data():
s = requests.Session()
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36'}
s.headers.update(headers)
resp = s.get('https://www.nseindia.com/market-data/live-equity-market')
resp.raise_for_status()
resp = s.get('https://www.nseindia.com/api/equity-stockIndices?csv=true&index=NIFTY%2050')
resp.raise_for_status()
data_79 = resp.text
data_79 = resp.text.replace('","', '')
with open('___N50__.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerows([line.split(',') for line in data_79.splitlines()])
if __name__ == "__main__":
Download_Fresh_data2()
The headers are all in rows rather than one column.
I have tried to arrive at this code after lot of learning, but it is still insufficient knowledge from my end. Kindly help!
3
Answers
If the url you are accessing has a valid CSV file, then you can direcly read the csv into pandas dataframe and then save to local machine as follows:
Did you take a look at the data with a text editor?
resp.text
seems to start with a BOM, and each field of the header ends with a linefeed. IMHO the data needs some cleaning:One possibility is to skip the header rows and explicitly add them: