finance using python DataFrame

here is my code

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)

what i want to see is:

    price change ex-dividend-date volume

awr

dov

nwn

emr

etc…

then i fixed my code like this

    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)

but it print as….for example

    price change EX-dividend-date volume

awr NaN NaN NaN NaN

it just print nothing at all!! so. what should i do get right output.. please help me. how can i handle this problem

I really appreciate your help:)!!!

Answer

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

Leave a Reply

Your email address will not be published. Required fields are marked *