【Python】Selenium輔助海量基金資料獲取
阿新 • • 發佈:2022-05-05
今天主要給大家介紹一下Selenium測試工具,它是一款瀏覽器測試專用的工具,能夠模擬使用者對瀏覽器進行滑鼠點選、頁面滑動等功能。一方面能夠用於網頁測試,另一方面能夠輔助網頁內容爬取(例如通過滑動頁面置底通過Ajax自動載入的頁面內容,或通過點選Button才能下載的內容等)。
本文主要介紹Selenium輔助海量基金資料的獲取。以騰訊證券為例進行說明(網址:http://stockhtm.finance.qq.com/fund/jzzx/index.htm),對應頁面如下所示:
通過上圖可以看到,我們需要在頁面輸入歷史資料對應的時間,以及按鍵“輸出到EXCEL”才能匯出對應的歷史資料。需要使用Selenium,(1)找到歷史資料的context控制元件,輸入對應的時間;(2)找到“輸出到EXCEL”的Button控制元件名,並按下Button控制元件。
- HTML頁面檢視,找到各控制元件名稱(Context對應"textfield",Button對應"Submit02")
- 獲取2017年3-8月半年的基金資料。得到各日期,針對每一天進行輸入日期、匯出資料到Excel操作。
elem = driver.find_element_by_name("textfield") #通過id找到日期輸入框的 elem.clear() #內容清空 elem.send_keys(date) #date是輸入的日期 elem1 = driver.find_element_by_name("Submit01") #確定輸入的日期 action1 = ActionChains(driver).move_to_element(elem1) action1.click(elem1) action1.perform() elem2 = driver.find_element_by_name("Submit02") #匯出資料到excel action2 = ActionChains(driver).move_to_element(elem2) action2.click(elem2) action2.perform()
3. 資料視覺化,通過使用baidu的echart工具,對獲取的資料進行視覺化展示。
(1)所有基金增長每日總量的變化趨勢圖:可以看出八月份(最後三十天)基金大幅度增長
(2)每天增長的基金數量佔所有基金比例的變化趨勢,可以看出80-144天時超過半數的基金在漲。
(3)每天所有基金價格增長的總和如下:
程式碼附錄:
# -*- coding: utf-8 -*- from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.action_chains import ActionChains dates = [] Stri = "20170" Mon = [1,3,5,7,8] for i in range(3,9): if i in Mon: N = 31 else: N = 30 for j in range(1,N+1): if j <10: tmpS = '0'+str(j) else: tmpS = str(j) dates.append(Stri+str(i)+tmpS) driver = webdriver.Chrome() driver.get("http://stockhtm.finance.qq.com/fund/jzzx/index.htm") elem = driver.find_element_by_name("textfield") for dat in dates: elem.clear() elem.send_keys(dat) elem1 = driver.find_element_by_name("Submit01") action1 = ActionChains(driver).move_to_element(elem1) action1.click(elem1) action1.perform() elem2 = driver.find_element_by_name("Submit02") action2 = ActionChains(driver).move_to_element(elem2) action2.click(elem2) action2.perform() # -*- coding: utf-8 -*- import os import numpy as np import pandas as pd from pyecharts import Line def Getfile(dirName): ''' 獲取資料夾資料 ''' files = os.listdir(dirName) return files def ReName(path,files): ''' 檔案重新命名 ''' for f in files: os.rename(dirName+f,dirName+f[9:]) def PlotLine(line,datx,daty,Marker): ''' 繪製線型影象 ''' #line = Line(Title) line.add(Marker,datx,daty, mark_point=["average","max"]) #return line #line.show_config() #line.render() def CalIncRate(NewFiles): ''' 計算每日增長基金佔比 ''' dayInc = [] dayIncVal = [] mk = 0 for f in NewFiles: mk += 1 print mk data = pd.read_html(dirName+f) Inc = data[0][5][1:] IncVal = map(float,data[0][4][1:]) dayIncVal.append(sum(IncVal)) cnt = 0 for rate in Inc: if float(rate) > 0: cnt += 1 dayInc.append(cnt/float(len(Inc))) return dayInc,dayIncVal,np.cumsum(dayIncVal) def CalIncNum(NewFiles): ''' 計算每日總基金增長點 ''' dayIncNum = [] return dayIncNum dirName = u'./股票型基金/' NewFiles = Getfile(dirName) dayInc,dayIncVal,dayIncCum = CalIncRate(NewFiles) line = Line("每日增長基金佔比","3月-8月") PlotLine(line,range(len(dayInc)),dayInc,"比例") line.show_config() line.render('h1.html') line = Line("每日基金增長總量","3月-8月") PlotLine(line,range(len(dayInc)),dayIncVal,"增長量") line.show_config() line.render('h2.html') line = Line("基金累計增長總量","3月-8月") PlotLine(line,range(len(dayInc)),list(dayIncCum),"增長累積量") line.show_config() line.render('h3.html')