0
点赞
收藏
分享

微信扫一扫

基金收益排名

左小米z 2022-02-05 阅读 88

基金收益排名

import xlwings as xw
import requests
from bs4 import BeautifulSoup
from datetime import datetime
import json
import xlwt
import xlwings as xw
from selenium import webdriver
import time
import pandas as pd
import csv
import re
from selenium.webdriver import Chrome, ChromeOptions, ActionChains
import multiprocessing

def web(url):
    driver.get(url)
    driver.refresh()
    time.sleep(0.5)
    #获取返回值
    source = driver.page_source  # 获取网页内容
    html = BeautifulSoup(source, 'html.parser')  # 获取网页内容
    # 写入网页
    with open('FundRank.html', 'w', encoding='utf-8-sig') as f:
        f.write(source)
    print('写入网页')
    return html
def data(html):
    df=pd.DataFrame()
    fundName=[]
    html.list=html.find_all('div',attrs={'class','ti-left'})[1]
    # print(html.list)
    for i,item in enumerate(html.list):
        try:
            if item.text!='\n':
                fundName.append(item.text)
                print(item.text)
        except:
            print('基本信息错误')
    df['基金名称'] = fundName[0],
    df['基金代码'] = fundName[1],
    df['基金类型'] = fundName[2],
    df['基金风险'] = fundName[3],
    print(df)
    return df

def data_a(html):
    df=pd.DataFrame()
    fundIncrease=[]
    html.list=html.find('table',attrs={'class','m-table'})
    # print(html.list)
    for i,item in enumerate(html.list):
        try:
            # fundIncrease.append(item.text)
            print(item.find__all('table',attrs={'id','tableCtn'}))
            # print(item.find__all('tr'))
        except:
            print('基本信息错误')
    df['近1周'] = fundIncrease[0],
    df['近1月'] = fundIncrease[1],
    df['近3月'] = fundIncrease[2],
    df['近6月'] = fundIncrease[3],
    df['今年来'] = fundIncrease[3],
    df['近1年'] = fundIncrease[3],
    df['近2年'] = fundIncrease[3],
    df['近3年'] = fundIncrease[3],

    print(df)
    return df


if __name__=="__main__":

    #打开excel
    app=xw.App(visible=True,add_book=False)
    wb=app.books.open('FundRank.xlsx')
    #连接excel
    sh=wb.sheets['FundRank']
    rng=[i for i in sh.range('E:E').value if i!=None]
    j=sh.range('a1').expand('table').rows.count
    app.display_alerts=False
    app.screen_updating=False
    # 打开网页
    opt = ChromeOptions()
    opt.headless = False
    driver = Chrome(options=opt)
    # driver.set_window_size(400,900)


    for i in range(len(rng)-1):
        try:
            #提取数据并查询
            time1=time.time()#开始计时
            Fundname=rng[i+1]#'003298'
            url='http://fund.10jqka.com.cn/'+Fundname+'/'
            url_increase='https://fund.10jqka.com.cn/public/newfund/syrank.html#'+Fundname#获取涨幅
            # html=web(url)#获取网页信息
            html_increase = web(url_increase)  # 获取网页信息
            # df1=data(html)#获取基金基本信息
            df2=data_a(html_increase)#获取基金涨幅
            # print(html)

            time2=time.time()
            print('总耗时{}'.format(time2-time1))
        except:
            print(str(i),'错误')


    try:
        wb.save('FundRank.xlsx')
        wb.close()
        app.quit()
        ''''''
        # 获得当前窗口句柄
        sreach_windows = driver.current_window_handle
        driver.quit()
        # 获得当前所有打开的窗口的句柄
        all_handles = driver.window_handles
        for handle in all_handles:
            driver.switch_to.window(handle)
            driver.close()
            time.sleep(2)
        driver.close()
        driver.quit()
        ''''''
    except:
        print('有错误代码')

举报

相关推荐

0 条评论