0

I used the codes below to extract historical data from nasdaq.com but it failed.

import pandas as pd link = "https://www.nasdaq.com/api/v1/historical/BFC/stocks/2015-07-12/2020-07-12" data = pd.read_csv(link, skiprows=2) print(data) 

Then I tried another code below but also failed.

import csv import requests csv_url = 'https://www.nasdaq.com/api/v1/historical/BFC/stocks/2015-07-12/2020-07-12/' req = requests.get(csv_url) url_content = req.content csv_file = open('downloaded.csv', 'wb') csv_file.write(url_content) csv_file.close() 

But the codes above work for some other url links like https://www.ishares.com/de/professionelle-anleger/de/produkte/270048/ishares-msci-world-value-factor-ucits-etf/1478358465952.ajax?fileType=csv&fileName=IS3S_holdings&dataType=fund&asOfDate=20180731

Anyone could advise?

1

1 Answer 1

0

You need to set a header with user-agent to the request The header should be in dict format

headers = {'User-Agent': 'Mozilla/5.0'} 

So your code with requests will be:

import csv import requests headers = {'User-Agent': 'Mozilla/5.0'} csv_url = 'https://www.nasdaq.com/api/v1/historical/BFC/stocks/2015-07-12/2020-07-12/' req = requests.get(csv_url, headers=headers) url_content = req.content csv_file = open('downloaded.csv', 'wb') csv_file.write(url_content) csv_file.close() 
Sign up to request clarification or add additional context in comments.

2 Comments

amazing! it works. got to know that the user-agent is created to fake the website.
Plz vote and confirm that my answer solve your problem @C.C

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.