求助-基金净值能显示,却不能正确写入excel文件中

大虾Help !

以下代码能执行,但只能在屏幕上显示净值,
现在想存入excel文件jjdata2022.xls中,但写进去的只是英文表头,
应该是从这一句开始
book=xlwt.Workbook(encoding='utf-8')#创建工作簿
哪儿有问题呢?

谢谢大虾先!^_^

import re,xlwt
import requests
from bs4 import BeautifulSoup
from prettytable import *

def get_url(url, params=None, proxies=None):
rsp = requests.get(url, params=params, proxies=proxies)
rsp.raise_for_status()
return rsp.text

def get_fund_data(code, start='', end='',i=''):
record = {'Code': code}
url = 'http://fund.eastmoney.com/f10/F10DataApi.aspx'
params = {'type': 'lsjz', 'code': code, 'page': i, 'per': 65535, 'sdate': start, 'edate': end}

page:per指定了每页的显示条数,存在一页显示不完整的情况,该参数直接指定显示第几页。'per': 65535,

html = get_url(url, params)
soup = BeautifulSoup(html, 'html.parser')
records = []
tab = soup.findAll('tbody')[0]
for tr in tab.findAll('tr'):
if tr.findAll('td') and len((tr.findAll('td'))) == 7:
record['Date'] = str(tr.select('td:nth-of-type(1)')[0].getText().strip())
record['NetAssetValue'] = str(tr.select('td:nth-of-type(2)')[0].getText().strip())
record['ChangePercent'] = str(tr.select('td:nth-of-type(4)')[0].getText().strip())
records.append(record.copy())
return records

def demo(code, start, end):
record = {'Code': code}
url = 'http://fund.eastmoney.com/f10/F10DataApi.aspx'
params = {'type': 'lsjz', 'code': code, 'per': 65535, 'sdate': start, 'edate': end}

page:per指定了每页的显示条数,存在一页显示不完整的情况,该参数直接指定显示第几页。'per': 65535,

# html = get_url(url, params)

url='http://fund.eastmoney.com/f10/F10DataApi.aspx?type=lsjz&code=110022&page=1&sdate=2005-01-01&edate=2021-02-13&per=20'

a=requests.get(url,params=params)
a.encoding='gbk'
b=a.text
ss=""
title=re.findall('pages:'+"\d*",str(b))

print (len(title))

for i in range(len(title)):

ss=ss+str(title)
#txt.write(' '+str(title)+'\n')

txt.close()

print (ss)
print (len(ss)) #str(title)
print (ss[8:len(ss)-2]) #[start:] 从start 提取到结尾
allpage=int(ss[8:len(ss)-2])
table = PrettyTable()
table.field_names = ['Code', 'Date', 'NAV', 'Change']
table.align['Change'] = 'r'

book=xlwt.Workbook(encoding='utf-8')#创建工作簿
sheet=book.add_sheet('fund1',cell_overwrite_ok=True)#创建表fund1,cell_overwrite_ok=True用于确认同一个celL单元是否可以重设置
head=['Code', 'Date', 'NAV', 'Change'] #定义表头,即Excel中第一行标题
for h in range(len( head) ) :
sheet.write(0,h,head[h])#写入表头
j=1#第—行开始
for i in range(1,allpage+1,1):
records = get_fund_data(code, start, end,i)

for list in records:
k =0
for date in list:
sheet.write(j, k,date)#迭代列,并写入数据,重新设置,需要cell_overwrite_ok=True
k = k+1
j = j+1
book.save('jjdata2022.xls')
for record in records:
table.add_row([record['Code'], record['Date'], record['NetAssetValue'], record['ChangePercent']])
print(table)
return table

if __name__ == "__main__":
demo('110022', '2022-01-01', '2022-01-31')
发表时间 2022-01-10 14:18

赞同来自:

0

jjroad

赞同来自:

加上后list[date]后终于成了,

^_^

多谢大虾们 3x 3x
2022-01-10 15:22修改 引用
0

看看不回

赞同来自:

python不太熟
for循环sheet.write那段,你print看看,是不是没执行
2022-01-10 14:52 引用
0

baoxiao

赞同来自:

直接搞成dataframe 然后用toexcle写
2022-01-10 14:48 引用
0

放羊娃

赞同来自:

这个问题,该发到CSDN上
2022-01-10 14:32 引用

要回复问题请先登录注册

发起人

问题状态

  • 最新活动: 2022-01-10 15:14
  • 浏览: 2399
  • 关注: 4