CSV文件,我寫了這段代碼:寫表在Python
import urllib.request
import re
import csv
from bs4 import BeautifulSoup
stocklist = ['aapl','goog','fb','amzn','COP']
for stocklist in stocklist:
optionsUrl = urllib.request.urlopen('http://finance.yahoo.com/q?s='+stocklist).read()
soup = BeautifulSoup(optionsUrl)
optionsTable = [
[x.text for x in y.parent.contents]
for y in soup.findAll('td', attrs={'class': 'yfnc_tabledata1','rtq_table': ''})
]
print(optionsTable)
length = len(optionsTable[0])
with open('test.csv', 'w', newline='') as fp:
a = csv.writer(fp, delimiter=',')
for y in range(length):
a.writerow([x[y] for x in optionsTable])
我想寫輸出到CSV文件,但我可以只寫「締約方會議的股票信息「,而不是其他4種股票的信息,打印輸出正確的行數..然後,我怎麼只有頭一次和數據5次。另外,如何在寫入CSV時添加名爲Symbol的新列。 這樣的事情:
符號[['Close Close:'],['Open:',],['Bid:'], ['Ask:'],['1y Target Est:',],['Beta:'],['Earnings Date:'],[「Day's Range:」],['52wk Range:'],['' Volume:',],['Avg Vol(3m):'],['Market Cap:'],['P/E(ttm):'],['EPS(ttm):'],['Div &收益率:'],['期貨市盈率(1年):'],['P/S(ttm):],['除息日期:'],['Annual EPS Est \ n(Sep -16)\ n:'],['Quarterly EPS Est \ n(Jun-16)\ n:'],['Mean Recommendation *:'],['PEG Ratio(預計5年):]]
以下是輸出:
[['Prev Close:', '98.94'], ['Open:', '98.50'], ['Bid:', '99.74 x 1500'], ['Ask:', '99.75 x 1400'], ['1y Target Est:', '124.90'], ['Beta:', '1.48679'], ['Earnings Date:', 'Jul 19 - Jul 25 (Est.)'], ["Day's Range:", '98.47 - 99.99'], ['52wk Range:', '89.47 - 132.97'], ['Volume:', '23,937,011'], ['Avg Vol (3m):', '38,273,600'], ['Market Cap:', '546.10B'], ['P/E (ttm):', '11.10'], ['EPS (ttm):', '8.98'], ['Div & Yield:', '2.28 (2.31%) '], ['Forward P/E (1 yr):', '10.95'], ['P/S (ttm):', '2.38'], ['Ex-Dividend Date:', '05-May-16'], ['Annual EPS Est\n (Sep-16)\n :', '8.28'], ['Quarterly EPS Est\n (Jun-16)\n :', '1.39'], ['Mean Recommendation*:', '1.8'], ['PEG Ratio (5 yr expected):', '1.29']]
[['Prev Close:', '728.28'], ['Open:', '723.71'], ['Bid:', '728.40 x 1300'], ['Ask:', '728.67 x 100'], ['1y Target Est:', '924.83'], ['Beta:', '1.032'], ['Next Earnings Date:', 'N/A'], ["Day's Range:", '722.34 - 729.54'], ['52wk Range:', '515.18 - 789.87'], ['Volume:', '837,958'], ['Avg Vol (3m):', '1,787,830'], ['Market Cap:', '500.30B'], ['P/E (ttm):', '29.65'], ['EPS (ttm):', '24.58'], ['Div & Yield:', 'N/A (N/A) '], ['Forward P/E (1 yr):', '18.37'], ['P/S (ttm):', '6.41'], ['Ex-Dividend Date:', 'N/A'], ['Annual EPS Est\n (Dec-16)\n :', '33.60'], ['Quarterly EPS Est\n (Jun-16)\n :', '8.06'], ['Mean Recommendation*:', '1.8'], ['PEG Ratio (5 yr expected):', '1.31']]
[['Prev Close:', '118.39'], ['Open:', '118.13'], ['Bid:', '118.60 x 300'], ['Ask:', '118.61 x 1900'], ['1y Target Est:', '142.87'], ['Beta:', '0.840485'], ['Earnings Date:', 'Jul 27 - Aug 1 (Est.)'], ["Day's Range:", '117.71 - 118.68'], ['52wk Range:', '72.00 - 121.08'], ['Volume:', '12,644,305'], ['Avg Vol (3m):', '25,848,800'], ['Market Cap:', '339.02B'], ['P/E (ttm):', '72.49'], ['EPS (ttm):', '1.64'], ['Div & Yield:', 'N/A (N/A) '], ['Forward P/E (1 yr):', '25.90'], ['P/S (ttm):', '17.13'], ['Ex-Dividend Date:', 'N/A'], ['Annual EPS Est\n (Dec-16)\n :', '3.56'], ['Quarterly EPS Est\n (Jun-16)\n :', '0.81'], ['Mean Recommendation*:', '1.7'], ['PEG Ratio (5 yr expected):', '0.96']]
[['Prev Close:', '726.64'], ['Open:', '723.10'], ['Bid:', '727.45 x 100'], ['Ask:', '727.59 x 100'], ['1y Target Est:', '800.92'], ['Beta:', '1.6465'], ['Earnings Date:', 'Jul 21 - Jul 25 (Est.)'], ["Day's Range:", '722.30 - 728.91'], ['52wk Range:', '422.64 - 731.50'], ['Volume:', '1,970,078'], ['Avg Vol (3m):', '3,982,600'], ['Market Cap:', '343.40B'], ['P/E (ttm):', '300.00'], ['EPS (ttm):', '2.43'], ['Div & Yield:', 'N/A (N/A) '], ['Forward P/E (1 yr):', '73.41'], ['P/S (ttm):', '3.02'], ['Ex-Dividend Date:', 'N/A'], ['Annual EPS Est\n (Dec-16)\n :', '5.38'], ['Quarterly EPS Est\n (Jun-16)\n :', '1.10'], ['Mean Recommendation*:', '1.8'], ['PEG Ratio (5 yr expected):', '2.43']]
[['Prev Close:', '47.49'], ['Open:', '46.72'], ['Bid:', '46.69 x 2900'], ['Ask:', '46.70 x 2700'], ['1y Target Est:', '51.23'], ['Beta:', '1.42252'], ['Earnings Date:', 'Jul 28 - Aug 1 (Est.)'], ["Day's Range:", '46.55 - 47.14'], ['52wk Range:', '31.05 - 64.24'], ['Volume:', '4,646,126'], ['Avg Vol (3m):', '9,070,990'], ['Market Cap:', '57.76B'], ['P/E (ttm):', 'N/A'], ['EPS (ttm):', '-4.98'], ['Div & Yield:', '1.98 (4.35%) '], ['Forward P/E (1 yr):', '150.64'], ['P/S (ttm):', '2.15'], ['Ex-Dividend Date:', '18-May-16'], ['Annual EPS Est\n (Dec-16)\n :', '-2.26'], ['Quarterly EPS Est\n (Jun-16)\n :', '-0.67'], ['Mean Recommendation*:', '2.5'], ['PEG Ratio (5 yr expected):', '0.38']]
謝謝
嘗試'csv.DictWriter':https://docs.python.org/2/library/csv.html#csv.DictWriter –
我建議你使用熊貓庫。我看到你正在處理股市數據,我也做了一個小項目。此外,csv寫熊貓將幫助您進行更深入的數據分析。如果您正在與雅虎金融合作,那麼還有一個可用的模塊。 – Eular
我在這個例子中使用 – showri