I am trying to download historical stock prices of AAPL from yahoo query but failed. I am very new to python and do not know which method to apply in auto extracting the designated link into my desired local directory.
pullData("AAPL") with result: AAPL https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=345398400&period2=1505577600&interval=1d&events=history&crumb=X44gAJPyoNu main loop HTTP Error 401: Unauthorized
import urllib2
import time
import csv
import requests
import pandas as pd
def pullData(stock):
try:
print stock
url = 'https://query1.finance.yahoo.com/v7/finance/download/'+stock+'?period1=345398400&period2=1505577600&interval=1d&events=history&crumb=X44gAJPyoNu'
print url
response = urllib2.urlopen(url)
html = response.read()
except Exception,e:
print 'main loop',str(e)