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?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
C.C
  • 27
  • 5
  • 3
    Does this answer your question? [Use python requests to download CSV](https://stackoverflow.com/questions/35371043/use-python-requests-to-download-csv) – joshua.software.dev Jul 11 '20 at 20:06

1 Answers1

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()
UWTD TV
  • 910
  • 1
  • 5
  • 11