简体   繁体   中英

How to get the yahoo finance csv directly into python

Does anybody know how to get yahoo finance csv directly into python?

The problem is that when i try to get the data with this (example) link:

http://real-chart.finance.yahoo.com/table.csv?s=WU&a=4&b=20&c=2015&d=05&e=21&f=2016&g=d&ignore=.csv'

It gives a pop-up asking if i want to download the csv-file. This causes it to bugg when i try to read it in to python. My scripts is:

today = datetime.date.today()

def get_url(stock='GOOG', START_date = str(int(str(today).split('-')[0])-1)+
            '-' +str(int(str(today).split('-')[1])-1) + ('-') +
            str(int(str(today).split('-')[2])-1), END_date= str(today)):
    baseurl = 'http://real-chart.finance.yahoo.com/table.csv?'
    stock = 's=WU'

    FROM_date = ('&a=' + START_date.split('-')[1] + '&b=' +
    START_date.split('-')[2] + '&c=' +
    START_date.split('-')[0])

    TO_date = ('&d=' + END_date.split('-')[1] + '&e=' +
    END_date.split('-')[2] + '&f=' + END_date.split('-')[0])

    url = baseurl + stock + FROM_date + TO_date + '&g=d&ignore=.csv'
    return url

rawdate = []
with open(get_url()) as csvfile:
    reader = csv.reader(csvfile, delimiter = ",")
    for row in reader:
        rawdata.append(row)

If i download the csv first i can read it into python, but I want to get to access the csv file directly without having to download it first. Is this possible? alternatively have the csv as temp.

Thanks!

I would recommend that you use pandas. Here is a link .

import pandas.io.data as web
import datetime
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2013, 1, 27)
f = web.DataReader("F", 'yahoo', start, end)
f.ix['2010-01-04']

Out[6]: Open 1.017000e+01

High 1.028000e+01

Low 1.005000e+01

Close 1.028000e+01

Volume 6.085580e+07

Adj Close 8.755953e+00 Name: 2010-01-04 00:00:00, dtype: float64

Try it this way.

in this file "C:/Users/your_path/Desktop/symbols/tickers.txt"
you have the following tickers
ibm
sbux
msft
"""

import urllib
import re
import json

symbolslist = open("C:/Users/rshuell001/Desktop/symbols/tickers.txt").read()
symbolslist = symbolslist.split("\n")

for symbol in symbolslist:
    myfile = open("C:/Users/rshuell001/Desktop/symbols/" +symbol +".txt", "w+")
    myfile.close()

    htmltext = urllib.urlopen("http://www.bloomberg.com/markets/chart/data/1D/"+ symbol+ ":US")
    data = json.load(htmltext)
    datapoints = data["data_values"]

    myfile = open("C:/Users/rshuell001/Desktop/symbols/" +symbol +".txt", "a")
    for point in datapoints:
        myfile.write(str(symbol+","+str(point[0])+","+str(point[1])+"\n"))
    myfile.close()

That should give you what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM