python - 使用 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 南 南 南 南 南
它什么也没打印!!所以。我应该怎么做才能得到正确的输出..请帮助我。我该如何处理这个问题
我真的很感谢你的帮助:)!!!
解决方案
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
推荐阅读
- c# - 在 Unity 中使用 Google Play 游戏插件登录时出现登录错误(代码 16)
- python - XML / Python - 在先前出现的元素和更新值之后插入元素的深层副本
- bitcoind - 无法构建比特币
- java - OutOfMemoryError:使用 Orika 1.5.4 的元空间
- apache-flink - 如何在 Flink 上启用 SSL
- php - 尝试在 PHP 中访问多个数组时如何防止迭代数组?
- spring - Spring拦截器获取真实的requestURI
- python - 烧瓶路由中的多个“路径”参数导致解析不正确
- php - 带解构的 PHP 中的选择排序
- java - 带有opendjdk 8的eclipse ide的MAC OS Big Sur问题