Python:循环遍历变量列表

标签 python python-2.7 yahoo-finance

我需要将多个 ticker 符号循环到我正在构建的 URL 中。我不确定这是否是解决此问题的最佳方法。

YahooFinance 网址生成器

从日期时间导入日期时间

def ConstructionYFURL(ticker,start_date,end_date,freq): start_date = datetime.strptime(start_date,"%Y-%m-%d").date() end_date = datetime.strptime(end_date, "%Y-%m-%d").date()

s=ticker.replace("^","%5E")
if start_date.month-1<10:
    a="0"+str(start_date.month-1)
else:
    a=str(start_date.month-1)

b=str(start_date.day)

c=str(start_date.year)

if end_date.month-1<10:
    d="0"+str(end_date.month-1)
else:
    d=str(end_date.month-1)


e=str(end_date.day)

f=str(end_date.year)

g=freq

yfURL = "http://real-chart.finance.yahoo.com/table.csv?s="+s+"&a="+a+"&b="+b+"&c="+c+"&d="+d+"&e="+e+"&f="+f+"&g="+g+"&ignore=.csv"
return yfURL


webRequest = urllib2.Request(urlOfFile)

try:
    page=urllib2.urlopen(webRequest)

    content=page.read()

    with open(filePath,"wb") as output:
        output.write(bytearray(content))



except urllib2.HTTPError, e:
    print e.fp.read()

ticker = ["FB","C"]

for x in ticker:
    start_date= "2015-01-01"
    end_date= "2017-02-23"
    freq = "d"
    yfURL = constructYFURL(ticker,start_date,end_date,freq)
    fileName="YF_"+str(ticker)+".csv"
    print yfURL
    localFilePath="/Users/student/Downloads/datalibrary/"
    download(localFilePath+fileName,yfURL)

最佳答案

这应该有助于让你们更接近。 urlOfFile 未定义,并且 download() 不是 python 模块,因此您必须修复该问题。如果您注释掉这些部分,它现在可以正确构建 URL。

import urllib2
from datetime import datetime

def constructYFURL(x, start_date, end_date, freq): 
    start_date = datetime.strptime(start_date, '%Y-%m-%d').date()
    end_date = datetime.strptime(end_date, '%Y-%m-%d').date()
    s = x.replace('^', '%5E')

    if (start_date.month - 1) < 10:
        a = '0' + str(start_date.month - 1)
    else:
        a = str(start_date.month - 1)

    b = str(start_date.day)
    c = str(start_date.year)

    if (end_date.month - 1) < 10:
        d = '0' + str(end_date.month - 1)
    else:
        d = str(end_date.month - 1)

    e = str(end_date.day)
    f = str(end_date.year)
    g = freq

    yfURL = 'http://real-chart.finance.yahoo.com/table.csv?s='+s+'&a='+a+'&b='+b+'&c='+c+'&d='+d+'&e='+e+'&f='+f+'&g='+g+'&ignore=.csv'
    return yfURL

def main():
    webRequest = urllib2.Request(urlOfFile)
    try:
        page = urllib2.urlopen(webRequest)
        content = page.read()
        with open(filePath,'wb') as output:
            output.write(bytearray(content))   
    except urllib2.HTTPError as e:
        print e.fp.read()

    ticker = ['fb', 'c', 'goog']
    start_date = '2015-01-01'
    end_date = '2017-02-23'
    freq = 'd'
    localFilePath = '/Users/student/Downloads/datalibrary/'

    for x in ticker:
        yfURL = constructYFURL(x, start_date, end_date, freq)
        fileName = 'YF_' + x + '.csv'
        print yfURL    
        download(localFilePath + fileName, yfURL)

if __name__ == '__main__':
    main()

关于Python:循环遍历变量列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42490664/

相关文章:

python - Matplotlib 绘图窗口出现在不同的桌面上

python - 在 Twisted 中将 HTTP 代理转换为 HTTPS 代理

c++ - 如何使用 C++ 的字节码优化来初始化嵌入式 Python 解释器?

python - 使用 boto3 清空 s3 存储桶的最快方法是什么?

r - 如何从雅虎(使用 Quantmod)获取 ETF 财务信息(例如 NAV)?

python - 为什么python的递归复制和命令行不一样?

python - 将 float 比率转换为整数

python-2.7 - 从数据存储区读取(跨组)实体时发生 TransactionFailedError(争用过多...)

iphone - JSON 雅虎财经图表数据 iOS 解析

android - 从路透社获取金融股关键发展链接