0

I am newbie to coding and I am trying to grab some history data from yahoo finance, and the code was work before

def crawl_price(stock_id):
        now = int(datetime.datetime.now().timestamp())+86400
        url = "https://query1.finance.yahoo.com/v7/finance/download/" + stock_id + "?period1=0&period2=" + str(now) + "&interval=1d&events=history&crumb=hP2rOschxO0"
        r = requests.post(url)
        f = io.StringIO(r.text)
        df = pd.read_csv(f, index_col='Date', parse_dates=['Date'])

        conn = sqlite3.connect('data/hkprice.db')
        df.to_sql('' + stock_id + '', conn, if_exists='replace')

        return df

but the output turns 'Date' is not in list

and i download the csv thought open with pandas, it show perfectly fine

i am not sure what going wrong

y2kmarkham
  • 35
  • 1
  • 7
  • my apologies, https://query1.finance.yahoo.com/v7/finance/download/0005.HK?period1=946857600&period2=1592352000&interval=1d&events=history – y2kmarkham Jun 17 '20 at 04:17

3 Answers3

0

Is it 'Date' with a capital letter or 'date' without a capital letter?
Could you give an example of the intended URL? It's hard to work without an example :/

0

Add seperator into the red_csv() method.

df = pd.read_csv(f, sep="," , index_col='Date', parse_dates=['Date'])

check the other parameters of read_csv like sep=";" , encoding="utf_8" etc.

  • i try ',' and';' , also the encoding to utf_8, but it seems not work as expected, still ValueError: 'Date' is not in list – y2kmarkham Jun 17 '20 at 06:13
  • Check this ---> [link](https://stackoverflow.com/questions/17465045/can-pandas-automatically-recognize-dates) – Sanmay Kokate Jun 17 '20 at 17:31
0

Have you tried making a GET request instead of a POST:

r = requests.get(url)

That URL does not allow POST requests. Here's the text returned when making the POST request:

r = request.post(url)
r.text
'{"finance":{"result":null,"error":{"code":"Method Not Allowed","description":"HTTP 405 Method Not Allowed"}}}'
putty
  • 744
  • 1
  • 6
  • 14
  • it's work!! thank you so much but how can i decide which one should i use in another scenario? – y2kmarkham Jun 18 '20 at 07:02
  • After you make the request, check the status code with r.status_code. A successful request will return 200. https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/200 – putty Jun 19 '20 at 01:54