張松 唐艷雙 李懷宇
[摘要]本文通過審計(jì)實(shí)例介紹運(yùn)用Go語言開發(fā)爬蟲程序,批量獲取某省金融企業(yè)監(jiān)管處罰數(shù)據(jù),對被審計(jì)單位及所在地域金融企業(yè)進(jìn)行全方位、立體畫像,實(shí)現(xiàn)網(wǎng)絡(luò)公開數(shù)據(jù)與行內(nèi)數(shù)據(jù)的相互補(bǔ)充印證,準(zhǔn)確鎖定審計(jì)目標(biāo),提升現(xiàn)場審計(jì)效率。
[關(guān)鍵詞]內(nèi)部審計(jì)? ? Go語言? ? 數(shù)據(jù)分析? ? 爬蟲程序
隨著信息科技與生產(chǎn)過程的不斷融合,人們越來越感受到科技在工作中的重要性。在企業(yè)內(nèi)部審計(jì)中,可利用數(shù)據(jù)分析語言開發(fā)網(wǎng)絡(luò)爬蟲程序獲取被審計(jì)單位的日常經(jīng)營及社會活動相關(guān)數(shù)據(jù)。Go語言(又稱Golang語言),是Google公司開發(fā)的一種簡潔高效的開源編程語言,具有高并發(fā)性和跨平臺開發(fā)優(yōu)勢,非常適合服務(wù)器編程。
一、總體思路
首先,開發(fā)的爬蟲程序必須善意且自覺遵守Robots協(xié)議,開發(fā)前要查看網(wǎng)站根目錄下的robots.txt文件,根據(jù)Robots協(xié)議合理進(jìn)行爬取數(shù)據(jù)。其次,將爬取到的監(jiān)管信息,存儲到CSV文本及本地MySQL數(shù)據(jù)庫中,并從中篩選出關(guān)注單位的處罰信息。
爬蟲程序工作原理是抓取網(wǎng)頁源代碼。源代碼中包含網(wǎng)頁內(nèi)容信息和網(wǎng)絡(luò)鏈接URL地址,可以繼續(xù)打開網(wǎng)頁中的URL地址,進(jìn)一步抓取下一層網(wǎng)頁內(nèi)容和網(wǎng)絡(luò)鏈接URL地址,這樣就可以循環(huán)抓取所需網(wǎng)頁公布的數(shù)據(jù)信息。
Go語言抓取網(wǎng)頁源代碼時,最常用的方法是利用Go語言自帶的net/http包工具與正則表達(dá)式相配合,匹配抓取想要的內(nèi)容信息,也可以利用一些根據(jù)網(wǎng)頁節(jié)點(diǎn)屬性選擇的CSS選擇器、Colly或Goquery等框架工具提取所需要的數(shù)據(jù)信息。
二、具體步驟
(一)分析網(wǎng)頁目標(biāo)地址,封裝地址函數(shù)
首先,明確爬取目標(biāo)網(wǎng)站的URL鏈接地址變化規(guī)律,需要爬取的內(nèi)容是金融機(jī)構(gòu)被監(jiān)管部門處罰的公開處罰信息;其次,打開具體的行政處罰頁面首頁發(fā)現(xiàn),每頁有18份處罰單的名稱和鏈接地址,打開第二頁、第三頁等多個行政處罰頁面,發(fā)現(xiàn)每頁都包含18份處罰單的鏈接,而且前三頁鏈接URL地址是一種類型格式,第四頁往后的網(wǎng)頁是另一種鏈接URL格式。所以將兩種鏈接URL格式定義為一個數(shù)組切片并賦值給urls變量,封裝到Get2Urlt函數(shù)中,用if語句自動判斷,當(dāng)頁碼小于4時取第一種URL格式,其他情況則取第二種URL格式,具體語句如下:
func Get2Urlt(idx int) (url string) {
urls :=
[]string{"http://www.****c.**v.cn/cn/st****/data/tocInfo/Selecr********ItemdId/data_itemId=1515,pageIndex=" + strconv.Itoa(idx) + ",pageSize=18.json",
"http://www.****c.**v.cn/dbircweb/tocInfo/Selecr********ItemdId?itemId=1515&pageSize=18&pageIndex=" + strconv.Itoa(idx)}
if idx < 4 {
url = urls[0]
} else {
url = urls[1]
}
return
}
(二)封裝函數(shù),獲取單個網(wǎng)頁信息
選用Go語言程序自帶的net/http包中的Get()方法,根據(jù)傳入的URL鏈接地址參數(shù)便可發(fā)起有效請求,獲取網(wǎng)頁源代碼并賦值給變量resp。因?yàn)镠ttp.Get()請求是一種網(wǎng)絡(luò)資源,用完后應(yīng)第一時間將其關(guān)閉,用defer指定的Close()函數(shù)來關(guān)閉網(wǎng)絡(luò)請求,語句如下:
func HttpGet(url string) (result []string, err error) {
resp, err1 := http.Get(url)
if err1 != nil {
err = err1
return
}
defer resp.Body.Close()
data1, err2 := ioutil.ReadAll(resp.Body)
b := UserJsonObj{}
json.Unmarshal([]byte(data1), &b)
if err2 != nil {
err = err2
return
}
urlls := make([]string, 0)
for i := 0; i <= 17; i++ {
urllist :=
"http://www.****c.**v.cn/branch/*******I/view/pages/tcommon/Item****t.html?docId=" + strconv.Itoa(int(b.Data.Rows[i].DocId)) + "&itemId=1515"
urlls = append(urlls, urllist)
}
result = urlls
return
}
(三)創(chuàng)建結(jié)構(gòu)體,存儲網(wǎng)絡(luò)Json數(shù)據(jù)
創(chuàng)建UserJsonObj結(jié)構(gòu)體,用來存儲網(wǎng)頁的Json數(shù)據(jù)并賦值給變量b,用json.Unmarshal工具將json字符串解碼到相應(yīng)的結(jié)構(gòu)體中。創(chuàng)建HttpGet()函數(shù)獲取每頁所有罰單的URL鏈接地址,循環(huán)讀出每份處罰單的鏈接URL地址,將所有罰單的鏈接URL地址賦值給變量urllist,并追加到數(shù)組切片urlls中,再將切片urlls賦值給result并返回GetOnePage()函數(shù),語句如下:
type UserJsonObj struct {
RptCode float64 `json:"rptCode"`
Msg? ? ?string? `json:"msg"`
Data? ? Data? ? `json:"data"`
}
type Data struct {
Total float64? ?`json:"total"`
Rows? []Brand `json:"rows"`
}
type Brand struct {
DocId? ? ? float64 `json:"tocId"`
DocSubtitle string `json:"tocSubtitle"`
PublishDate string `json:"publishDate"`
DocSummarystring`json:"tocSummary"`
DocFileUrl? string `json:"tocFileUrl"`
Generaltype string `json:"generaltype"`
PdfFileUrl? string `json:"pdfFileUrl"`
ItemName? string `json:"itemName"`
SolicitFlag string `json:"tolicitFlag"`
DocTitle? ? string `json:"docTitle"`
Datafrom? ?string `json:"datafrom"`
DocUuid? ? string `json:"docUuit"`
Builddate? ?string `json:"builddate"`
}
func GetOnePage(idx int, page chan int) {
url := Get2Urlt(idx)
authors, err := HttpGet(url)
if err != nil {
fmt.Println("HttpGet err:", err)
return
}
mc := make([]string, 0)
dw := make([]string, 0)
xm := make([]string, 0)
yy := make([]string, 0)
tk := make([]string, 0)
fk? := make([]string, 0)
rq? := make([]string, 0)
for _, jokeURL := range authors {
conUrl := GetUrlt(jokeURL)
contentUrl :=
"http://www.****c.****v.cn/cn/****ic/data/tocInfo/SelecR****t/data_docId=" + conUrl + ".json"
mc1, dw1, xm1, yy1, tk1, fk1, rq1, err := GetTwoPage(contentUrl)
if err != nil {
fmt.Println("Spider2Page err:", err)
continue
}
mc = append(mc, mc1)
dw = append(dw, dw1)
xm = append(xm, xm1)
yy = append(yy, yy1)
tk? = append(tk, tk1)
fk? = append(fk, fk1)
rq? = append(rq, rq1)
}
SpiderWrPage(idx, mc, dw, xm, yy, tk, fk, rq)
page <- idx
}
(四)封裝函數(shù),進(jìn)一步獲取罰單信息
創(chuàng)建GetTwoPage(contentUrl)函數(shù)獲取第二層中每份罰單的關(guān)鍵信息。傳入?yún)?shù)是加工后的contentUrl罰單鏈接地址,GetTwoPage函數(shù)也是使用net/http包中的Get()方法來獲取每張罰單的源代碼數(shù)據(jù),再用Goquery工具的doc.Find()方法對每張罰單文本中的表格數(shù)據(jù)進(jìn)行過濾提取,分別提取出罰單中的名稱、被處罰單位、被處罰人姓名、被處罰原因、違反條款、處罰信息和處罰日期等關(guān)鍵信息,將提取到的信息分別返回GetOnePage()函數(shù),語句如下:
func GetTwoPage(contentUrl string) (mc1, dw1, xm1, yy1, tk1, fk1, rq1 string, error) {
resp, err1 := http.Get(contentUrl)
if err1 != nil {
err = err1
return
}
fileContent := make([]string, 0)
fileContent0 := make([]string, 0)
doc,err := goquery.NewDocumentFromResponse(resp)
if err == nil {
doc.Find("td").Each(func(i int, s *goquery.Selection) {
str1 := s.Text()
str1 = strings.Replace(str1, " ", "", -1)
str1 = strings.Replace(str1, "_", "", -1)
str1 = strings.Replace(str1, "\\r\\n", "", -1)
str1 = strings.Replace(str1, "\r|\n", "", -1)
str1 = strings.Replace(str1, " ", "", -1)
fileContent = append(fileContent, str1)
})
} else {
fmt.Println("err--->", err)
}
if len(fileContent) < 2 {
doc.Find("span").Each(func(i int, s *goquery.Selection) {
str1 := s.Text()
str1 = strings.Replace(str1, " ", "", -1)
str1 = strings.Replace(str1, "_", "", -1)
str1 = strings.Replace(str1, "\n", "", -1)
str1 = strings.Replace(str1, "\r", "", -1)
str1 = strings.Replace(str1, "\\r\\n", "", -1)
str1 = strings.Replace(str1, "\r\n", "", -1)
str1 = strings.Replace(str1, " ", "", -1)
fileContent0 = append(fileContent0, str1)
})
}
hz1 := make([]string, 0)
hz0 := make([]string, 0)
for i := 0; i < len(fileContent); i++ {
fileContent[i] = strings.Replace(fileContent[i], " ", "", -1)
fileContent[i] = strings.Replace(fileContent[i], "\n", "", -1)
fileContent[i] = strings.Replace(fileContent[i], "\t", "", -1)
fileContent[i] = strings.Replace(fileContent[i], " ", "", -1)
Hz := fileContent[i]
hz1 = append(hz1, Hz)
}
for i := 0; i < len(fileContent0); i++ {
fileContent0[i] = strings.Replace(fileContent0[i], " ", "", -1)
fileContent0[i] = strings.Replace(fileContent0[i], "\n", "", -1)
fileContent0[i] = strings.Replace(fileContent0[i], "\t", "", -1)
fileContent0[i] = strings.Replace(fileContent0[i], " ", "", -1)
Hz0 := fileContent0[i]
hz0 = append(hz0, Hz0)
}
if len(hz1) >= 21 {
mc1 = hz1[1]
dw1 = hz1[10]
xm1 = hz1[12]
yy1 = hz1[14]
tk1 = hz1[16] + hz1[18]
fk1 = hz1[20]
rq1 = hz1[len(hz1)-1]
} else {
}
if len(hz0) >= 30 {
mc1 = hz0[1]
dw1 = hz0[13]
xm1 = hz0[10]
yy1 = hz0[19] + hz0[20] + hz0[21] + hz0[22] + hz0[23] + hz0[24] + hz0[25]
tk1? = hz0[28]
fk1? =? hz0[28] + hz0[29] +hz0[30]
rq1 = hz0[len(hz0)-9] + hz0[len
(hz0)-8] + hz0[len(hz0)-7] + hz0[len(hz0)-6] + hz0[len(hz0)-5] + hz0[len(hz0)-4] + hz0[len(hz0)-3]
} else {
}
return
}
(五)封裝函數(shù),將數(shù)據(jù)寫入文本
GetOnePage()函數(shù)接收到GetTwoPage()函數(shù)采集到的罰單關(guān)鍵數(shù)據(jù)后,分別將每個字段的數(shù)據(jù)信息追加到提前設(shè)置好的數(shù)組切片中,將收到的切片數(shù)據(jù)傳給創(chuàng)建的SpiderWrPage()函數(shù),將采集到的每頁數(shù)據(jù)逐條寫入文本中,多傳入一個idx參數(shù)記錄采集到第幾頁的數(shù)據(jù),用strconv.Itoa(idx)方法將整型數(shù)字轉(zhuǎn)換成字符串,配合目錄地址創(chuàng)建對應(yīng)的目錄文本文件,語句如下:
func SpiderWrPage(idx int, mc, dw, xm, yy, tk, fk, rq []string) {
//path := "D:/go/src/crawler/第1" + strconv.Itoa(idx) + "頁.txt"
path := "D:/go/src/go_tintjj.go/data1/第" + strconv.Itoa(idx) + "頁.txt"
f, err := os.Create(path)
if err != nil {
fmt.Println("os.Create err:", err)
return
}
defer f.Close()
n := len(mc)
for i := 0; i < n; i++ {
f.WriteString(strconv.Itoa(i+1) + "," + mc[i] + "," + dw[i] + "," + xm[i] + "," + yy[i] + "," + tk[i] + "," + fk[i] + "," + rq[i] + "\n")
}
MySql(idx)
}
(六)封裝函數(shù),將文本數(shù)據(jù)寫入本地MySQL數(shù)據(jù)庫
創(chuàng)建MySql()函數(shù),將寫入文本的罰單數(shù)據(jù)插入本地MySql數(shù)據(jù)庫中,運(yùn)行流程為:首先用sqlx.Open()函數(shù)打開本地MySql數(shù)據(jù)庫,用db.Exec()方法加create table語句創(chuàng)建對應(yīng)的數(shù)據(jù)表;其次用Go語言自帶os包中的os.Open()方法打開數(shù)據(jù)文本,用for循環(huán)語句里嵌套reader.ReadLine()方法逐行讀取數(shù)據(jù),直到io.EOF文件結(jié)束時停止;最后用db.Exec()方法加insert into語句將讀取到的數(shù)據(jù)逐條插入對應(yīng)數(shù)據(jù)表中,語句如下:
func MySql(idx int) {
db,err := sqlx.Open("mysql", "root:a123@tcp(127.0.0.1:3306)/test")
HandleError(err, "sql.Open")
defer db.Close()
_, err = db.Exec("create table if not exists data_ybjh(idx int(8),mc varchar(260),dw varchar(424),xm varchar(224),yy varchar(824),tk varchar(424),fk varchar(424),rq varchar(264));")
HandleError(err, "db.Exec create table")
fpath := GetFpath(idx)
file, e := os.Open(fpath)
HandleError(e, "os.Open")
defer file.Close()
reader := bufio.NewReader(file)
for {
lineBytes, _, err := reader.ReadLine()
HandleError(err, "reader.ReadLine")
if err == io.EOF {
break
}
lineStr := string(lineBytes)
fields := strings.Split(lineStr, ",")
idx, mc, dw, xm, yy, tk, fk, rq := fields[0], fields[1], fields[2], fields[3], fields[4], fields[5], fields[6], fields[7]
result, err := db.Exec("insert into data_tbjh(idx,mc,dw,xm,yy,tk,fk,rq) values(?,?,?,?,?,?,?,?);", idx, mc, dw, xm, yy, tk, fk, rq)
HandleError(err, "db.Exec insert")
if n, e := result.RowsAffected(); e == nil && n > 0 {
fmt.Printf("插入%s 數(shù)據(jù)成功!?。躰", mc)
if _, e := result.RowsAffected(); e != nil && e == io.EOF {
break
}
}
}
}
(七)數(shù)據(jù)入庫成功
程序運(yùn)行完畢后,打開本地MySQL數(shù)據(jù)庫查詢發(fā)現(xiàn),采集的金融單位處罰數(shù)據(jù)信息,包括名稱、被處罰單位、被處罰人姓名、被處罰原因、違反條款、處罰信息和處罰日期等關(guān)鍵信息,已分字段插入本地?cái)?shù)據(jù)庫對應(yīng)表中,達(dá)到預(yù)期程序設(shè)計(jì)要求,為審計(jì)項(xiàng)目的開展增加一條數(shù)據(jù)獲取渠道。
三、數(shù)據(jù)初步分析
通過SQL語句對MySQL數(shù)據(jù)庫中提取的處罰信息進(jìn)行匯總分析,發(fā)現(xiàn)被審計(jì)單位的主要監(jiān)管處罰原因主要集中于貸款資金被挪用、違規(guī)發(fā)放貸款、集團(tuán)客戶授信超比例及貸款五級分類不準(zhǔn)確等方面。
本次利用Go語言技術(shù)獲取監(jiān)管處罰信息過程中,在網(wǎng)頁文本數(shù)據(jù)篩選獲取時用到了Goquery框架工具的doc.Find方法。Goquery框架工具功能強(qiáng)大、使用靈活方便,在對文本數(shù)據(jù)篩選提取時也可使用regexp包中的MustCompile和FindAllStringSubmatch方法以達(dá)到同樣效果。
上述方法可以一次性獲取某個省份特定單位的監(jiān)管處罰信息,對被審計(jì)單位及所在地域金融企業(yè)的經(jīng)營情況有更加全面的了解,開闊審計(jì)人員的思路,豐富審計(jì)手段,提升現(xiàn)場審計(jì)效率。
(作者單位:中國郵政儲蓄銀行股份有限公司審計(jì)局沈陽分局,郵政編碼:110013,電子郵箱:cdzhangs@163.com)