Yahoo! Finance financial Data Python temporary Read method

Source: Internet
Author: User

This post was transferred from Pinterest: http://www.jianshu.com/p/85d563d326a9

This time in the quantitative strategy, found a relatively good open source project, but Yahoo financial data source has not been found, found on the Internet this article, share. At the bottom of the article is the original author's number, you want to reward yourself ~ ~

Yahoo! Finance provides financial data at home and abroad, and Python often uses pandas or matplotlib for data reading.

The original data interface was offline as Yahoo! 's API was upgraded unilaterally on May 16, 2017.

Original URL format: Https://chart.yahoo.com/table.csv?s=IBM
Now adjust to:https://query1.finance.yahoo.com/v7/finance/download/ibm?period1=1492611801&period2=1495203801& interval=1d&events=history&crumb=nmhmtcv7qpm


Yahoo.jpg

Before the pandas and matplotlib Yahoo Finance patches are released, data can be extracted through the temporary workaround provided in this article.

Original data extraction Method one:

import pandas.io.data as webIBMStock = web.DataReader(name="IBM", data_source="yahoo",start="2000-1-1")

Original data extraction Method two:

import requestss = requests.Session()r = s.get("https://chart.yahoo.com/table.csv?s=IBM",verify=False)

The original data extraction method three:

from matplotlib.finance import quotes_historical_yahoo_ochldate1=(2013, 1, 1)date2=(2013, 12,31)price=quotes_historical_yahoo_ochl(‘IBM‘, date1, date2)

Python Temporary Solution:
Step1. To get the cookie value when accessing Yahoo through the browser

Https://finance.yahoo.com/quote/IBM/history?p=IBM

Chrome


Cookies.jpg

Step2. Right click Download, get crumb value

https://query1.finance.yahoo.com/v7/finance/download/ibm?period1=1492611801&period2=1495203801& interval=1d&events=history&crumb=nmhmtcv7qpm


Crumb.jpg

Step3. Replace starting and ending dates with Unix time

Code sample (Python 2.7.13 | Anaconda 4.3.1 (64-bit)):

#-*-Coding:utf-8-*-"" "Created on Fri May 2017@author:vincentqiao" "" Import requestsimport Timeimport pandas as Pdim Port Matplotlib.pyplot as Pltdef datetime_timestamp (DT): time.strptime (DT, '%y-%m-%d%h:%m:%s ') S = Time.mktime (ti Me.strptime (DT, '%y-%m-%d%h:%m:%s ')) return str (int (s)) S = requests. Session () #Replace b=xxxxcookies = dict (b= ' c650m5hchrhii&b=3&s=tk ') #Replace crumb=yyyycrumb = ' NMHMTCV7QPM ' Begin = Datetime_timestamp ("2014-01-01 09:00:00") end = Datetime_timestamp ("2017-04-30 09:00:00") R = S.get ("https:// query1.finance.yahoo.com/v7/finance/download/ibm?period1= "+begin+" &period2= "+end+" &interval=1d& events=history&crumb= "+crumb,cookies=cookies,verify=false) F = open (' Ibm.csv ', ' W ') F.write (R.text) f.close () es = Pd.read_csv (' Ibm.csv ', Index_col=0,parse_dates=true, sep= ",", dayfirst=true) data = PD. DataFrame ({"IBM": es["Adj Close"] [:]}) Print (Data.info ()) Data.plot (Subplots=true, grid=true, style= "B", figsize= (8, 6 )) Plt.show ()

Operation Result:
?


Author of the public number


Qrcode_small.jpg

Yahoo! Finance financial Data Python temporary Read method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.