1. 程式人生 > >18-09-18 關於建行基金全域性函式的整合

18-09-18 關於建行基金全域性函式的整合

# 編譯日期:2018-08-23 15:03:20
# 版權所有:www.i-search.com.cn
# coding=utf-8


import xlwings as xw
import datetime
import os
import ubpa.iie as iie


#1建立目錄
def create_dir(fund_dir):
now_time = datetime.datetime.now()
yesterday = now_time + datetime.timedelta(days=-1)
yesterday = yesterday.strftime('%Y%m%d')
newdir="E:\\fund_data\\"+fund_dir+"\\pic"+str(yesterday)
if not os.path.exists(newdir):
os.makedirs(newdir)
#print('====newdir====',newdir)
return(newdir)

def move_pic(fund_dir,fund_name):
create_dir(fund_dir)
now_time = datetime.datetime.now()
yesterday = now_time + datetime.timedelta(days=-1)
yesterday = yesterday.strftime('%Y%m%d')
newdir="E:\\fund_data\\"+fund_dir+"\\pic"+str(yesterday) #新目錄是PIC+日期
path = "E:\\fund_data\\imgsss\\" #臨時目錄
dirs_pic = os.listdir(path) #把獲取的圖片名字按字母排序
#print(dirs_pic)
#對以上獲取按字母排序的目錄檔案列表排序 以是按照建立時間對檔案排序
oldfile_list=sorted(dirs_pic,key=lambda x: os.path.getctime(os.path.join(path,x
)))
#dir_list = sorted(dir_list,key=lambda x: os.path.getctime(os.path.join(DIR,x)))
oldfile = path+oldfile_list[0]
print("1oldfile======================================",oldfile)

#獲取臨時目錄下的檔案列表
newfile = newdir+"\\"+fund_name+".jpg"
#新檔名字,bankname是傳入的引數
#print(oldfile)
#print(newfile)
if os.path.exists(newfile): #必須先判斷目標目錄是否存在檔案
os.remove(newfile)
os.rename(oldfile,newfile)



#
#A獲取所有資料
def get_data_all():
for x in range(5, 21):
for y in range(1, 8):

iie.get_text(url=r'https://mail.qq.com/cgi-bin/readmail?folderid=1&folderkey=1&&t=readmail&mailid=ZC3015-abwZcwIFuIb1Oix3ne_ib88&mode=pre&maxage=3600&base=12.16&ver=15253&sid=1QARIVAIgb-Aemw3&newwin=true&nocheckframe=true',selector=r'#mailContentContainer > TBODY:nth-of-type(1) > TR:nth-of-type({0}) > TD:nth-of-type({1})'.format(x, y),waitfor=10)
print("所有資料獲取成功")

#A 第一行第一個資料和第二個資料
def get_data_line():
iie.get_text(url=r'http://www.scfund.com.cn/flagfund_2011/index.shtml',selector=r'DIV:nth-of-type(1) > DIV:nth-of-type(2) > DIV:nth-of-type(2) > DIV:nth-of-type(2) > DIV:nth-of-type(9) > TABLE:nth-of-type(1) > THEAD:nth-of-type(1) > TR:nth-of-type(1) > TH:nth-of-type(1)',waitfor=10)
iie.get_text(url=r'http://www.scfund.com.cn/flagfund_2011/index.shtml',selector=r'DIV:nth-of-type(1) > DIV:nth-of-type(2) > DIV:nth-of-type(2) > DIV:nth-of-type(2) > DIV:nth-of-type(9) > TABLE:nth-of-type(1) > THEAD:nth-of-type(1) > TR:nth-of-type(1) > TH:nth-of-type(2)',waitfor=10)
print("行資料獲取成功")

#A 第一列第一個資料和第二個資料
def get_data_col():

iie.get_text(url=r'http://www.scfund.com.cn/flagfund_2011/index.shtml',selector=r'DIV:nth-of-type(1) > DIV:nth-of-type(2) > DIV:nth-of-type(2) > DIV:nth-of-type(2) > DIV:nth-of-type(9) > TABLE:nth-of-type(1) > THEAD:nth-of-type(1) > TR:nth-of-type(1) > TH:nth-of-type(1)',waitfor=10)
iie.get_text(url=r'http://www.scfund.com.cn/flagfund_2011/index.shtml',selector=r'DIV:nth-of-type(1) > DIV:nth-of-type(2) > DIV:nth-of-type(2) > DIV:nth-of-type(2) > DIV:nth-of-type(9) > TABLE:nth-of-type(1) > TBODY:nth-of-type(1) > TR:nth-of-type(1) > TD:nth-of-type(1)',waitfor=10)
print("列資料獲取成功")


#東吳基金獲取資料1
def get_data_all_1():
list_data=[]
for x in range(1, 25): #每行 多少行
for y in range(1, 12): #每列 td 這個列數和擷取的列表要出去空格和空要大於擷取的個數
#iie.get_text(url=r'http://www.scfund.com.cn/flagfund_2011/index.shtml',selector=r'DIV:nth-of-type(1) > DIV:nth-of-type(2) > DIV:nth-of-type(2) > DIV:nth-of-type(2) > DIV:nth-of-type(9) > TABLE:nth-of-type(1) > TBODY:nth-of-type(1) > TR:nth-of-type(1) > TD:nth-of-type(1)',waitfor=10)

data = iie.get_text(url=r'http://www.scfund.com.cn/flagfund_2011/index.shtml',selector=r'DIV:nth-of-type(1) > DIV:nth-of-type(2) > DIV:nth-of-type(2) > DIV:nth-of-type(2) > DIV:nth-of-type(9) > TABLE:nth-of-type(1) > TBODY:nth-of-type(1) > TR:nth-of-type({0}) > TD:nth-of-type({1})'.format(x, y),waitfor=10)
if str(data).__contains__("型"):
continue
elif str(data) == '':
continue
elif str(data)== ' ':
continue
list_data.append(str(data))

#print(list_data)
return list_data


#東吳基金寫入資料
def write_data():
list1 = get_data_all_1()
#print(list1)
iters = [iter(list1)]*9
a_data = list(zip(*iters))
#print(a_data)

num = '2'
for v in a_data:
b_num = 'A'+str(num)
v = list(v) #本身是個生成器 不加list是不能寫入成功
#print('v',v)
wb = xw.Book(r"E:\fund_data\dongwu.xlsx")
sht = wb.sheets[0]
sht.range(b_num).value = v
num = str(int(num) + 1)
wb.save()

return ('寫入成功')

#def write_data1():
# a = [1,2,3,4,5,6]
# wb = xw.Book(r'C:\Users\WY\Desktop\sheet.xls')
# sht = wb.sheets[0]
#sht.range('A1').value = a
#wb.save()

#import ubpa.iimg as iimg
#def click():
#iimg.do_click_pos(win_title=r'QQ郵箱 - 收件箱 - Internet Explorer',img_res_path=r'C:\ueba\studio-v6\project\foun_text\screenshots',image=r'a.png',button=r'left',curson=r'Center',offsetX=100,offsetY=200,times=1,image_size=r'201X53',waitfor=30)


def get_data_line():
iie.get_text(url=r'https://www.wjasset.com/index.html',selector=r'#tabCot_jj_1 > TABLE:nth-of-type(1) > TBODY:nth-of-type(1) > TR:nth-of-type(1) > TH:nth-of-type(1)',waitfor=10)
iie.get_text(url=r'https://www.wjasset.com/index.html',selector=r'#tabCot_jj_1 > TABLE:nth-of-type(1) > TBODY:nth-of-type(1) > TR:nth-of-type(1) > TH:nth-of-type(2)',waitfor=10)

def get_data_col():
iie.get_text(url=r'https://www.wjasset.com/index.html',selector=r'#tabCot_jj_1 > TABLE:nth-of-type(1) > TBODY:nth-of-type(1) > TR:nth-of-type(1) > TH:nth-of-type(1)',waitfor=10)
iie.get_text(url=r'https://www.wjasset.com/index.html',selector=r'#tabCot_jj_1 > TABLE:nth-of-type(1) > TBODY:nth-of-type(1) > TR:nth-of-type(2) > TD:nth-of-type(1)',waitfor=10)

 

#萬家基金獲取資料
def get_data_all_w():
list_data_w=[]
for x in range(2,12): #每行 多少行
for y in range(1, 9): #每列 td 這個列數和擷取的列表要出去空格和空要大於擷取的個數
data = iie.get_text(url=r'https://www.wjasset.com/index.html',selector=r'#tabCot_jj_1 > TABLE:nth-of-type(1) >TBODY:nth-of-type(1) > TR:nth-of-type({0}) > TD:nth-of-type({1})'.format(x, y),waitfor=10)
list_data_w.append(data)
return list_data_w

#萬家基金寫資料
def write_data_w():
list_2 = get_data_all_w()
iters = [iter(list_2)]*8
w_data = list(zip(*iters))
print("w_data",w_data)
num = '2'
for v in w_data:
b_num = str("A"+ str(num))
v = list(v)
wb = xw.Book(r"E:\fund_data\wanjia.xlsx")
sht = wb.sheets[0]
sht.range(b_num).value = v
num = str(int(num)+1)
wb.save()
return "寫入成功"



#獲取中加基金資料1
def get_data_all_zj():
list_data_zj=[]
for x in range(2,29): #每行 多少行
for y in range(1, 10): #每列 td 這個列數和擷取的列表要出去空格和空要大於擷取的個數
data = iie.get_text(url=r'http://www.bobbns.com/',selector=r'#tableSort_0 > TBODY:nth-of-type(1) >TR:nth-of-type({0}) > TD:nth-of-type({1})'.format(x, y),waitfor=10)
if str(data).__contains__("\r\n"):
data = str(data).replace("\r\n",'')
list_data_zj.append(data)
else:
list_data_zj.append(data)
return list_data_zj

#寫入欠十行資料
def write_data_zj():
list_3 = get_data_all_zj()
iters = [iter(list_3)]*9
zj_data = list(zip(*iters))
print("zj_data",zj_data)
num = '2'
for v in zj_data:
b_num = str("A"+ str(num))
v = list(v)
wb = xw.Book(r"E:\fund_data\zhongjia.xlsx")
sht = wb.sheets[0]
sht.range(b_num).value = v
num = str(int(num)+1)
wb.save()
return "寫入成功"


#獲取中加基金資料2
def get_data_all_zj1():
list_data_zj=[]
for x in range(15,29): #每行 多少行
for y in range(1, 10): #每列 td 這個列數和擷取的列表要出去空格和空要大於擷取的個數
data = iie.get_text(url=r'http://www.bobbns.com/',selector=r'#tableSort_0 > TBODY:nth-of-type(1) >TR:nth-of-type({0}) > TD:nth-of-type({1})'.format(x, y),waitfor=10)
if str(data).__contains__("\r\n"):
data = str(data).replace("\r\n",'')
list_data_zj.append(data)
else:
list_data_zj.append(data)
return list_data_zj

#寫入後邊十行資料
def write_data_zj1():
list_4 = get_data_all_zj1()
iters = [iter(list_4)]*9
zj_data = list(zip(*iters))
print("zj_data",zj_data)
num = '15'
for v in zj_data:
b_num = str("A"+ str(num))
v = list(v)
wb = xw.Book(r"E:\fund_data\zhongjia.xlsx")
sht = wb.sheets[0]
sht.range(b_num).value = v
num = str(int(num)+1)
wb.save()
return "寫入成功"