缺少一个写文件的循环[英] Missing a loop to write a file

本文是小编为大家收集整理的关于缺少一个写文件的循环的处理方法,想解了缺少一个写文件的循环的问题怎么解决?缺少一个写文件的循环问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

import urllib.request
import re
import csv
import pandas as pd
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)
    stocksymbol = ['Symbol:',''+stocklist+'']
    optionsTable = [stocksymbol]+[
        [x.text for x in y.parent.contents]
        for y in soup.findAll('td', attrs={'class': 'yfnc_tabledata1','rtq_table': ''})
    ]
    print(optionsTable)
    my_df = pd.DataFrame(optionsTable).T
    my_df.to_csv('test.csv', index=False, header=False)

我有这段代码.有人建议我使用熊猫.我能够将列表中的数据写入 CSV 文件.但是 CSV 文件只有 COP 的数据,但没有其他股票的数据(csv 文件只有一行数据,我假设它正在覆盖).有人可以告诉我我缺少什么或修复此代码吗?print(optionsTable) 虽然打印了 4 行..

这是输出:

[['Symbol:', 'aapl'], ['Prev Close:', '99.65'], ['Open:', '98.51'], ['Bid:', '98.95 x 1700'], ['Ask:', '98.96 x 1200'], ['1y Target Est:', '124.90'], ['Beta:', '1.48679'], ['Earnings Date:', 'Jul 19 - Jul 25 (Est.)'], ["Day's Range:", '98.48 - 99.35'], ['52wk Range:', '89.47 - 132.97'], ['Volume:', '28,454,663'], ['Avg Vol (3m):', '38,261,900'], ['Market Cap:', '541.57B'], ['P/E (ttm):', '11.01'], ['EPS (ttm):', '8.98'], ['Div & Yield:', '2.28 (2.30%) '], ['Forward P/E (1 yr):', '10.86'], ['P/S (ttm):', '2.40'], ['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.30']]
[['Symbol:', 'goog'], ['Prev Close:', '728.58'], ['Open:', '719.47'], ['Bid:', '717.60 x 400'], ['Ask:', '717.96 x 100'], ['1y Target Est:', '924.83'], ['Beta:', '1.032'], ['Next Earnings Date:', 'N/A'], ["Day's Range:", '716.43 - 725.86'], ['52wk Range:', '515.18 - 789.87'], ['Volume:', '1,050,710'], ['Avg Vol (3m):', '1,781,050'], ['Market Cap:', '493.43B'], ['P/E (ttm):', '29.25'], ['EPS (ttm):', '24.58'], ['Div & Yield:', 'N/A (N/A) '], ['Forward P/E (1 yr):', 'N/A'], ['P/S (ttm):', '6.41'], ['Ex-Dividend Date:', 'N/A'], ['Annual EPS Est\n                      (Dec-16)\n                    :', 'N/A'], ['Quarterly EPS Est\n                      (Jun-16)\n                    :', 'N/A'], ['Mean Recommendation*:', '1.8'], ['PEG Ratio (5 yr expected):', 'N/A']]
[['Symbol:', 'fb'], ['Prev Close:', '118.56'], ['Open:', '117.52'], ['Bid:', '116.39 x 800'], ['Ask:', '116.40 x 500'], ['1y Target Est:', '142.87'], ['Beta:', '0.840485'], ['Earnings Date:', 'Jul 27 - Aug 1 (Est.)'], ["Day's Range:", '116.26 - 118.11'], ['52wk Range:', '72.00 - 121.08'], ['Volume:', '17,257,639'], ['Avg Vol (3m):', '25,746,700'], ['Market Cap:', '333.25B'], ['P/E (ttm):', '71.26'], ['EPS (ttm):', '1.64'], ['Div & Yield:', 'N/A (N/A) '], ['Forward P/E (1 yr):', '25.25'], ['P/S (ttm):', '17.16'], ['Ex-Dividend Date:', 'N/A'], ['Annual EPS Est\n                      (Dec-16)\n                    :', 'N/A'], ['Quarterly EPS Est\n                      (Jun-16)\n                    :', 'N/A'], ['Mean Recommendation*:', '1.7'], ['PEG Ratio (5 yr expected):', 'N/A']]
[['Symbol:', 'amzn'], ['Prev Close:', '727.65'], ['Open:', '722.35'], ['Bid:', '716.25 x 500'], ['Ask:', '716.50 x 100'], ['1y Target Est:', '800.92'], ['Beta:', '1.6465'], ['Earnings Date:', 'Jul 21 - Jul 25 (Est.)'], ["Day's Range:", '714.21 - 724.98'], ['52wk Range:', '422.64 - 731.50'], ['Volume:', '3,161,899'], ['Avg Vol (3m):', '3,948,360'], ['Market Cap:', '338.47B'], ['P/E (ttm):', '295.70'], ['EPS (ttm):', '2.43'], ['Div & Yield:', 'N/A (N/A) '], ['Forward P/E (1 yr):', '72.29'], ['P/S (ttm):', '3.03'], ['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']]
[['Symbol:', 'COP'], ['Prev Close:', '46.57'], ['Open:', '45.90'], ['Bid:', '44.47 x 1300'], ['Ask:', '44.48 x 2300'], ['1y Target Est:', '51.23'], ['Beta:', '1.42252'], ['Earnings Date:', 'Jul 28 - Aug 1 (Est.)'], ["Day's Range:", '44.26 - 46.12'], ['52wk Range:', '31.05 - 64.13'], ['Volume:', '8,217,057'], ['Avg Vol (3m):', '8,947,330'], ['Market Cap:', '55.11B'], ['P/E (ttm):', 'N/A'], ['EPS (ttm):', '-4.98'], ['Div & Yield:', '1.98 (4.16%) '], ['Forward P/E (1 yr):', '143.48'], ['P/S (ttm):', '2.11'], ['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.37']]

推荐答案

每次循环时都会覆盖 csv.您应该收集所有数据并在循环后将它们写入 csv:

stocklist = ['aapl','goog','fb','amzn','COP']
columns = []
data = []
for s in stocklist:
    optionsUrl = urllib.request.urlopen('http://finance.yahoo.com/q?s='+s).read()
    soup = BeautifulSoup(optionsUrl, "html.parser")
    stocksymbol = ['Symbol:', s]
    optionsTable = [stocksymbol]+[
    [x.text for x in y.parent.contents]
    for y in soup.findAll('td', attrs={'class': 'yfnc_tabledata1','rtq_table': ''})
    ]

    if not columns:
        columns = [o[0] for o in optionsTable]
    data.append(o[1] for o in optionsTable)

# create DataFrame from data
df = pd.DataFrame(data, columns=columns)
df.to_csv('test.csv', index=False)

本文地址:https://www.itbaoku.cn/post/1727774.html