首页 > 解决方案 > 使用 python DataFrame 进行财务

问题描述

这是我的代码

from pandas import Series, DataFrame
import datetime
import requests
import lxml
import yfinance as yf
import time
from requests.exceptions import ConnectionError
from bs4 import BeautifulSoup



def web_content_div(web_content,class_path):
    web_content_div = web_content.find_all('div',{'class': class_path})
    try:
        spans = web_content_div[0].find_all('span')
        texts = [span.get_text() for span in spans]
       
    except IndexError:
        texts = []
    
    return texts

def real_time_price(stock_code):
    
    url = 'https://finance.yahoo.com/quote/' + stock_code + '?p=' + stock_code 
   
    try :
        r = requests.get(url)
        web_content = BeautifulSoup(r.text,'lxml')
        texts = web_content_div(web_content, 'My(6px) Pos(r) smartphone_Mt(6px)')
        if texts != []:
            price, change = texts[0],texts[1]
        else:
            price , change = [] , []
    
    
        texts = web_content_div(web_content,'D(ib) W(1/2) Bxz(bb) Pstart(12px) Va(t) ie-7_D(i) ie-7_Pos(a) smartphone_D(b) smartphone_W(100%) smartphone_Pstart(0px) smartphone_BdB smartphone_Bdc($seperatorColor)')
        if texts != []:
            for count, EX in enumerate(texts):
                if EX == 'Ex-Dividend Date':
                    EXdate = texts[count + 1]
        else:
            EXdate = []

        texts = web_content_div(web_content,'D(ib) W(1/2) Bxz(bb) Pend(12px) Va(t) ie-7_D(i) smartphone_D(b) smartphone_W(100%) smartphone_Pend(0px) smartphone_BdY smartphone_Bdc($seperatorColor)')
        if texts != []:
            for count, vol in enumerate(texts):
                if vol == 'Volume':
                    volume = texts[count + 1]
        else:
            volume = []


    except ConnectionError:
        price, change,EXdate,volume = [],[],[],[]

    return price, change, EXdate,volume

stock=['awr','dov','nwn','emr','gpc','pg','ph','mmm','ginf','jnj','ko','lanc','low','fmcb'
'cl','ndsn','hrl','abm','cwt','tr','frt','scl','swk','tgt','cbsh','mo','syy']


while(True):
    info = []
    col = []
    for stock_code in stock:
        time_stamp = datetime.datetime.now() - datetime.timedelta(hours=10)
        time_stamp = time_stamp.strftime('%Y-%M-%D %H:%M:%S')
        price,change,EXdate,volume = real_time_price(stock_code)
        info.append(price)
        info.extend([change])
        info.extend([EXdate])
        info.extend([volume])
        time.sleep(1)
    col = [time_stamp]
    col.extend(info)
    df = DataFrame(col, columns=['price', 'change', 'EX-dividend-date', 'Volume'], index=stock)
    df.T

    print(col)

我想看到的是:

    price change ex-dividend-date volume

awr

多夫

nwn

电子病历

ETC...

然后我像这样修复了我的代码

    while(True):
        info_price = []
        info_change = []
        info_exdate = []
        info_volume = []
        for stock_code in stock:
            price,change,EXdate,volume = real_time_price(stock_code)
            info_price.append(price)
            info_change.append(change)
            info_exdate.append(EXdate)
            info_volume.append(volume)
            time.sleep(1)
        df = DataFrame(columns={"price":info_price,"change":info_change,"EX-dividend-date":info_exdate,"volume":info_volume},index=stock)
        df.T
    
        print(df)

但它打印为....例如

    price change EX-dividend-date volume

awr 南 南 南 南 南

它什么也没打印!!所以。我应该怎么做才能得到正确的输出..请帮助我。我该如何处理这个问题

我真的很感谢你的帮助:)!!!

标签: pythonpandasdataframe

解决方案


stock=['awr','dov','nwn','emr','gpc','pg','ph','mmm','ginf','jnj','ko','lanc','low','fmcb'
'cl','ndsn','hrl','abm','cwt','tr','frt','scl','swk','tgt','cbsh','mo','syy']


while(True):
    info_timestamp = []
    info_price = []
    info_change = []
    info_exdate = []
    info_volume = []
    for stock_code in stock:
        time_stamp = datetime.datetime.now() - datetime.timedelta(hours=10)
        time_stamp = time_stamp.strftime('%Y-%M-%D %H:%M:%S')
        price,change,EXdate,volume = real_time_price(stock_code)
        info_timestamp.append(time_stamp)
        info_price.append(price)
        info_change.append(change)
        info_exdate.append(EXdate)
        info_volume.append(volume)
        time.sleep(1)
    df = DataFrame({"timestamp":info_timestamp,"price":info_price,"change":info_change,"EX-dividend-date":info_exdate,"volume":info_volume},index=stock)
    df.T

推荐阅读