我想要做的是通过将股票的雅虎财务的价格放入电子表格中来获得这一行代码。我问了很多人,我已经研究了这个问题,但是我还没有得到它的工作。从Python写入Excel到Excel
这是代码的麻烦行:
ws.cell(1,i+1).value = str.replace(price','price')
我得到的错误喜欢,“替换”需要‘STR’对象,但收到名单'或‘诠释’对象有没有属性,每次我修改它。感谢提前的帮助。
from openpyxl import Workbook
import urllib
import re
from openpyxl.cell import get_column_letter
wb = Workbook()
dest_filename = r'empty_book.xlsx'
ws = wb.create_sheet()
ws.title = 'Stocks'
symbolslist = ["aapl","spy","goog","nflx"]
i=0
while i<len(symbolslist):
#counts each object as 1 in the list
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
ws.cell(1,i+1).value = str.replace(price','price')
i+=1
wb.save(filename = dest_filename)
导入CSV模块比使用Excel进行数据分析,因为它是开源的友好更好。
这是一个有趣的想法......但我在哪里把这些代码?我想要Excel文件以后处理数据。 –