我试图弄清楚如何将数据添加到 Excel 电子表格中,openpyxl
但我还没有想出将值写入单个 Excel 单元格。
我想要做的是将循环中的打印结果保存到 Excel 的第一四行并将其保存为fundamental_data
. 这是代码。如果你知道任何好的教程,请告诉我openpyxl
。
from openpyxl import Workbook
import urllib
import re
symbolslist = ["aapl","spy","goog","nflx"]
from openpyxl import load_workbook
wb2 = load_workbook('fundamental_data.xlsx')
i=0
while i<len(symbolslist):
url = "http://finance.yahoo.com/q?s="+symbolslist[i]+"&q1=1"
htmlfile = urllib.urlopen(url)
htmltext = htmlfile.read()
regex = '<span id="yfs_l84_'+symbolslist[i]+'">(.+?)</span>'
pattern = re.compile(regex)
price = re.findall(pattern,htmltext)
print "The price of", symbolslist[i], " is ", price
i+=1
wb = Workbook()
wb.save('fundamental_data.xlsx')