简体   繁体   English

如何将Yahoo Finance CSV直接导入python

[英]How to get the yahoo finance csv directly into python

Does anybody know how to get yahoo finance csv directly into python? 有人知道如何将Yahoo Finance CSV直接导入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. 它给出一个弹出窗口,询问我是否要下载csv文件。 This causes it to bugg when i try to read it in to python. 当我尝试将其读入python时,这会导致bugg。 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. 如果我先下载csv,我可以将其读入python,但是我想直接访问csv文件,而不必先下载它。 Is this possible? 这可能吗? alternatively have the csv as temp. 或者将csv作为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 出[6]:打开1.017000e + 01

High 1.028000e+01 高1.028000e + 01

Low 1.005000e+01 低1.005000e + 01

Close 1.028000e+01 关闭1.028000e + 01

Volume 6.085580e+07 音量6.085580e + 07

Adj Close 8.755953e+00 Name: 2010-01-04 00:00:00, dtype: float64 调整关闭8.755953e + 00名称: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. 那应该给你你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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