massringo
massringo
采纳率33.3%
2021-05-04 22:10

我现在是读取了一个基金的全部数据,但是第二行就是重复了第一个基金的全部数据,是哪里逻辑错了

15
from rpa.core import *
from rpa.utils import *
import rpa4 as rpa # 使用V4引擎
import os

global page0
def start():
    path=r'D:\基金.xlsx'
    excelFile=os.path.exists(path)
    if excelFile ==False:
        newExcel(path)
    fundlist=['LU0095030564','LU0040709171','LU0102830865','LU0496384180','LU0430678424','LU0060230025','LU0057025933','LU0493246853','LU0861579265','LU0231459107','LU0132414144','LU0094547139','LU0455925619','LU0568621618','LU0823040885','LU0839530044','LU0823046494','HK0000220308','IE00BZ122929','IE00BZ127Z37','IE00BZ127T76','LU0216734045','IE0000830236','IE0030165983','IE0000805634','IE0000838304','IE0000829238','IE0000838189','LU0414403419','LU0821914370','LU0063729296','LU0359201612','LU0093503810','LU0171277485','LU0011846440','LU0072462426','LU0238689110','LU0072463663','LU0035112944','LU0124384867','LU0122376428','LU0171305526','LU0122379950','LU0075056555','LU0265266980','LU0048573561','LU0261945553','LU0205439572','LU0286668453','LU0261950983','LU0173614495','LU0303823156','LU0261953490','LU0119124781','LU0048578792','LU0261948227','LU0251132253','LU0055114457','LU0049112450','LU0261947096','LU0048621477','LU0048622798','LU0261952922','LU0109394709','LU0260871552','IE00B000C709','LU0352132103','LU0109981661','LU0109391861','IE0009570106','IE0008368742','LU0367026217','LU0075112721','IE0004445015','IE0009355771','IE0033534557','LU0229494975','LU0264606111','LU0327786744','LU0070992663','LU0011889929','LU1611731263','LU0210527791','LU0499112034','LU0431992006','LU0210526801','LU0344579056','HK0000155504','HK0000055811','LU0853555380','IE00B19Z9Z06','IE00B19Z4B17','IE00B2Q1FD82','LU0384381660','LU0426412945','LU0345770308','LU0280430660','LU0101692670','LU0128467544','LU0386859887','LU0217139020','LU0338482267','LU0256846139','LU0340557775','LU0104884860','IE00B12V2V27','IE00B2N6FJ21','IE00B1B80R65','HK0000087129','HK0000036530','IE0032590923','LU0106250508','LU0228659784','LU0244354667','LU0181495838','LU0106817157','LU0106253197','LU0106235533','LU0106235293','LU0302445910','LU0269904917','LU0256331488','LU1223082196','LU0180781048','LU0757359368','LU0140636845','LU0149534421','LU0903425923','LU0270814014','LU0205193047','LU0128522157','LU0390136223','HK0000127438','LU0390136736','LU0252652382','LU0128526141','LU0170475312','LU0086177085','LU0328353924','LU0198837287','LU0878005551','HK0000288735','HK0000068285']
    for i in fundlist:
        page = rpa.app.chrome.create('https://www.morningstar.hk/hk/screener/fund.aspx#?filtersSelectedValue=%7B%22term%22:%22'+i+'%22%7D')
        homePage()
        page0=fundPage()
    shutil.copy(path10, r'./结果.xlsx')

def homePage():
    sleep(10)
    page = rpa.app.chrome.catch('Morningstar', mode='title', pattern='contain')
    sleep(2)
    aud=page.count('参与者身份确认')
    if aud:
        page.click('选择', index = 1, simulate = True, button = 'left')
        page.click('接受', index = 1, simulate = True, button = 'left')
 
def fundPage():
    global page0
    page = rpa.app.chrome.catch('Morningstar Asia', mode='title', pattern='contain')
    sleep(2)
    aud=page.count('参与者身份确认')
    if aud:
        page.click('选择', index = 1, simulate = True, button = 'left')
        page.click('接受', index = 1, simulate = True, button = 'left')
    sleep(2)
    l=page.value('中文')
    if l=='中文':
        page.click('中文', index = 1, simulate = True, button = 'left')
    page1 = page
    S = "无数据"
    fund=page.count('基金链接')
    if fund>1:
        for i in range(1,fund+1):
            aud=page.count('参与者身份确认')
            if aud:
                page.click('选择', index = 1, simulate = True, button = 'left')
                page.click('接受', index = 1, simulate = True, button = 'left')
                sleep(2)
            fundLink=page.click('基金链接',index=i, simulate=False)
            url = "https://www.morningstar.hk" + page.attr('基金链接', 'href', index = 1)            
            page0 = rpa.app.chrome.create(url)
            performedData=fundPerformedData(page0)
            performedDataExcel(performedData)
            investmentData=fundInvestmentData(page0)
            investmentDataExcel(investmentData)
            page0.close()
    else:
        aud=page.count('参与者身份确认')
        if aud:
            page.click('选择', index = 1, simulate = True, button = 'left')
            page.click('接受', index = 1, simulate = True, button = 'left')
        sleep(2)
        url = "https://www.morningstar.hk" + page.attr('基金链接', 'href', index = 1)
        page0 = rpa.app.chrome.create(url)
        performedData=fundPerformedData(page0)
        performedDataExcel(performedData)
        investmentData=fundInvestmentData(page0)
        investmentDataExcel(investmentData)
        page0.close()
    page1.close()
       

def fundPerformedData(page0):
    performedData=[]
    page = page0
    sleep(2)    
    aud=page.count('参与者身份确认')
    if aud:
        page.click('选择', index = 1, simulate = True, button = 'left')
        page.click('接受', index = 1, simulate = True, button = 'left')
    sleep(2)
    page.click('表现', index = 1, simulate = True, button = 'left')
    if page.count("基本信息"):
        basicInformationValue=page.value('基本信息',index = 2)
    else:
        basicInformationValue="无数据"
    if page.count('总回报'):
        totalReturnValue=page.table('alirpa-token="0/6"', 'html', 'text')
    else:
        totalReturnValue="无数据"

    if page.count('追踪回报'):
        trackingReturnsValue=page.table('alirpa-token="0/8"', 'html', 'text')
    else:
       trackingReturnsValue="无数据"

    performedData.append((basicInformationValue,totalReturnValue,trackingReturnsValue))
    return performedData
   

def fundInvestmentData(page0):
    investmentData=[]
    page = page0
    page.click('投资组合', index = 1, simulate = True, button = 'left')

    quotation=page.count('报价')
    if quotation:
        quotationValue=page.value('报价',index = 2)
    else:
        quotationValue="无数据"

    assetAllocation=page.count("资产配置")
    if assetAllocation:    
        assetAllocationValue=page.table('alirpa-token="0/148"', 'html', 'text')
    else:
        assetAllocationValue="无数据"

    bondStyle=page.count('债券风格')
    if bondStyle:
        bondStyleValue=page.table('alirpa-token="0/204"', 'html', 'text')
    else:
        bondStyleValue="无数据"

    distribution=page.count('分布')
    if distribution:
        distributionValue=page.table('alirpa-token="0/278"', 'html', 'text')
    else:
        distributionValue="无数据"
    
    bondClassification=page.count('债券分类')
    if bondClassification:
        bondClassificationValue=page.table('alirpa-token="0/292"', 'html', 'text')
    else:
        bondClassificationValue="无数据"

    shareholding=page.count('持股')
    if shareholding:
        shareholdingValue=page.value('持股',index = 2)
    else:
        shareholdingValue="无数据"
        investmentData.append((quotationValue,assetAllocationValue,bondStyleValue,distributionValue,bondClassificationValue))
    return investmentData
    print(len(investmentData))

def newExcel(path):
    xlsx = rpa.app.microsoft.excel.create()
    xlsx.add_sheet('基金表现',location='Sheet1')
    xlsx.add_sheet('投资组合(行业与比重)',location='Sheet2')
    xlsx.save('./Test.xlsx')

def performedDataExcel(performedData):
    xlsx=rpa.app.microsoft.excel.open(r'D:\基金.xlsx')
    sht=xlsx.get_sheet('基金表现')
    for i in performedData:
        for j in range(1,300):
            sht.write('A'+str(j),str(i))
    xlsx.save(file='D:\基金.xlsx')
    xlsx.close 

def investmentDataExcel(investmentData):
    xlsx=rpa.app.microsoft.excel.open(r'D:\基金1.xlsx')
    sht=xlsx.get_sheet('投资组合(行业与比重)')
    for i in investmentData:
        for j in range(1,len(investmentData)+1):
            
            sht.write('A'+str(j),str(i))
    xlsx.save(file='D:\基金1.xlsx')
    xlsx.close
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

相关推荐