我是Python新手,我想创建一个执行以下操作的程序:
使用此list = [aapl,goog,jnj,msft]
作为输入
转到网址"https://finance.google.com/finance/historical?q=goog&output=csv"
并将"goog"
替换为上面列表中的代码
最后将CSV文件保存到文件系统中。
图片包含我能够想到的代码
from urllib import request
ticker_list = "aapl"
response = request.urlretrieve("https://finance.google.com/finance/historical?q="+ticker_list+"&output=csv",'D:/LatestStockFile.csv')
#[aapl,goog,xom,ge,msft,bp,c,pg,wmt,pfe,tm,jnj]
# I want tickers from this list to append instead of ticker_list
# also if possible a different file name to save for each iteration of ticker
# example : say ticker = "aaple" then this "aapl" should be appended to url and path: "D:/Apple.csv"
def download_csv(ticker,path):
response = request.urlretrieve(ticker,path)
def user_input():
choice = input("enter choice: 1/2/3\n")
if choice == 1:
download_csv("aapl","D:/Apple.csv")
elif choice == 2:
download_csv=("goog","D:/Google.csv")
elif choice == 3:
download_csv("jnj","D:/Johnson.csv")
user_input()
答案 0 :(得分:2)
import requests
if __name__ == '__main__':
list = ["aapl", "goog", "jnj", "msft"]
for items in list:
req = requests.get('https://finance.google.com/finance/historical?q=' + items + '&output=csv')
with open(items+".csv","w") as file:
file.write(req.text)
答案 1 :(得分:0)
GarfieldCat的答案有利于其用例,但并未直接解决您将库存名称作为CSV文件名的请求。它只是股票名称。
您可以做的是定义字典键值数据结构以保存公司名称和股票名称。例如,将现有列表调整为:
stocks = [
{
'ticker': 'aapl',
'company: 'Apple'
},
{
'ticker: 'goog',
'company: 'Google'
},
...
...
]
然后使用GarfieldCat中的代码,仅适用于从字典中提取数据:
for item in stocks:
url = 'https://finance.google.com/finance/historical?q={}&output=csv'.format(item.get('ticker'))
req = requests.get(url)
with open('{}.csv'.format(item.get('company')), 'wb') as f:
f.write(req.text)
如果您仍然对此有疑问,我建议您查看request
库文档,了解如何从请求中获取数据,然后阅读Python写入文件。
答案 2 :(得分:0)
这样做怎么样?
"""
"""
#in this file "C:/Users/rshuell001/Desktop/symbols/amex.txt"
#you have the following tickers
#ibm
#sbux
#msft
"""
import urllib
import re
import json
symbolslist = open("C:/Users/rshuell001/Desktop/symbols/amex.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()
这是一个类似但不同的选项(您正在读取元组而不是文本文件)。
import urllib2
listOfStocks = ["AAPL", "MSFT", "GOOG", "FB", "AMZN"]
urls = []
for company in listOfStocks:
urls.append('http://real-chart.finance.yahoo.com/table.csv?s=' + company + '&d=6&e=28&f=2015&g=m&a=11&b=12&c=1980&ignore=.csv')
Output_File = open('C:/Users/rshuell001/Historical_Prices.csv','w')
New_Format_Data = ''
for counter in range(0, len(urls)):
Original_Data = urllib2.urlopen(urls[counter]).read()
if counter == 0:
New_Format_Data = "Company," + urllib2.urlopen(urls[counter]).readline()
rows = Original_Data.splitlines(1)
for row in range(1, len(rows)):
New_Format_Data = New_Format_Data + listOfStocks[counter] + ',' + rows[row]
Output_File.write(New_Format_Data)
Output_File.close()