我正在尝试从Yahoo Finance数据中删除选定的列。我能够以csv格式抓取整个数据,但我很想知道如何只抓取选定的列而不是整个csv数据。我尝试使用split方法将字符串数据转换为列表,然后仅从列表中访问所需的列,但它无法正常工作。
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('../Files_Directory/Yahoo_Finance/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()
答案 0 :(得分:1)
使用现有的Yahoo Finance python模块可能会让您的生活变得更轻松,例如“yahoo_finance”
使用此模块(未测试)只写出体积数据
import yahoo_finance as yf
import csv
listOfStocks = ["AAPL", "MSFT", "GOOG", "FB", "AMZN"]
with open('my_output') as csvfile:
Output_file = csv.writer(csvfile)
for stock in listOfStocks:
s = yf.Share(stock)
hist = s.get_historical('2015-01-01', '2015-10-30')
for row in hist:
Output_file.writerow([stock, row['Date'], row['Volume'])