我在创建表是动态调整到不同的结果的问题。
我写了一个屏幕刮刀从拉股票http://finance.yahoo.com和打印公司名称,它的象征,它是当前的股票价格。
然而,输出如下所示:
Microsoft Corporation MSFT 29.76
Apple Inc. AAPL 396.77
SPDR S&P 500 SPY 155.25
Google Inc. GOOG 787.76
我希望它看起来像
Microsoft Corporation MSFT 29.76
Apple Inc. AAPL 396.77
SPDR S&P 500 SPY 155.25
Google Inc. GOOG 787.76
我刚开始使用Python昨天工作呢和我使用3.3.1
我当前的代码如下:
import re
import urllib.request
import cgi
from bs4 import BeautifulSoup
price = [0,0,0,0]
namesList = ["string1", "string2", "string3", "string4"]
stocksList = ["msft","aapl","spy","goog"]
def HTML():
i = 0
while i < len(stocksList):
htmlPull = urllib.request.urlopen("http://finance.yahoo.com/q?s="+stocksList[i]+"&ql=1")
htmlPull = htmlPull.read().decode('utf-8')
regex = '<span id="yfs_l84_'+stocksList[i]+'">(.+?)</span>'
pattern = re.compile(regex)
price[i] = re.findall(pattern,htmlPull)
htmlParse = BeautifulSoup(htmlPull)
title = htmlParse.title.contents
namesList[i] = title
i+=1
formatPrice(price)
formatStock(namesList)
formatOutput(namesList, stocksList, price)
def formatPrice(price):
k=0
while k < len(price):
cleaner = str(price[k])
cleaner = cleaner.replace("[","")
cleaner = cleaner.replace("]","")
cleaner = cleaner.replace("'","")
price[k] = float(cleaner)
k+=1
def formatStock(namesList):
k = 0
while k <len(namesList):
capital = stocksList[k]
capital = capital.upper()
cleaner = str(namesList[k])
cleaner = cleaner.replace("Summary for ", "")
cleaner = cleaner.replace(":"," ")
cleaner = cleaner.replace("- Yahoo! Finance'","")
cleaner = cleaner.replace("['","")
cleaner = cleaner.replace("]","")
cleaner = cleaner.replace(";","")
cleaner = cleaner.replace(capital, "")
namesList[k] = cleaner;
k+=1
def formatOutput(namesList, stocksList, price):
i = 0
while i < len(price):
capital = stocksList[i]
capital = capital.upper()
print(namesList[i],capital, price[i])
print("")
i+=1
HTML()
已经尝试打印({0},{1},{2} .format(namesList,资本,价格[1])),不同类型的{:<16}的变化等,这似乎只影响一个线和我“M好一会才想到在列的条款,或表,或可能的空间一定量的文本和空格应该填写。 我不知道确切的解决方案是在这里什么,所以我要问你所有的:)
你可以通过我的代码告诉,我很新的节目,所以如果有一个更好的方法在此代码做任何事情,我很乐意听校正,建议和意见。