【go语言爬虫】网贷天眼数据平台爬虫

来源:互联网 发布:蓝桥杯单片机怎么准备 编辑:程序博客网 时间:2024/05/02 04:46

一、需求分析
利用go语言抓取网贷天眼数据平台昨日数据
字段:
排序 平台名称 成交额 综合利率 投资人 借款周期 借款人 满标速度 累计贷款余额 资金净流入
抓取url:
http://www.p2peye.com/shuju/ptsj/

二、go语言爬虫实现源代码

package mainimport (       "fmt"       "io/ioutil"       "net/http"       "time"       "os"       "regexp"       "github.com/axgle/mahonia")//定义新的数据类型type Spider1 struct {       url    string       header map[string]string}//定义 Spider get的方法func (keyword Spider1) get_html_header() string {       client := &http.Client{}       req, err := http.NewRequest("GET", keyword.url, nil)       if err != nil {       }       for key, value := range keyword.header {              req.Header.Add(key, value)       }       resp, err := client.Do(req)       if err != nil {       }       defer resp.Body.Close()       body, err := ioutil.ReadAll(resp.Body)       if err != nil {       }       return string(body)}func parse1() {       header := map[string]string{              "Host":"www.p2peye.com",              "Connection":"keep-alive",              "Cache-Control":"max-age=0",              "Upgrade-Insecure-Requests":"1",              "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",              "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",              "Referer":"http://www.p2peye.com/shuju/hysj/",              "Accept-Language":"zh-CN,zh;q=0.8",       }       //创建excel文件       f, err := os.Create("C:/p2p.xlsx")       if err != nil {              panic(err)       }       defer f.Close()       //写入标题       f.WriteString("排序"+"\t"+"平台名称"+"\t"+"成交额"+"\t" +"综合利率"+"\t"   +"投资人"+"\t"+"借款周期"+"\t"+"借款人"+"\t"+"满标速度"+"\t"+"累计贷款余额"+"\t"+"资金净流入"+"\r\n")       url := "http://www.p2peye.com/shuju/ptsj/"       spider := &Spider1{url, header}       html := spider.get_html_header()       //设置编码格式       decoder := mahonia.NewDecoder("gbk")       html1:=decoder.ConvertString(html)       //排序       pattern1 := `<td class="num">(.*?)</td>`       rp1 := regexp.MustCompile(pattern1)       find_txt1 := rp1.FindAllStringSubmatch(html1, -1)       //平台名称       pattern2 := ` onclick="return false" title="(.*)">`       rp2 := regexp.MustCompile(pattern2)       find_txt2 := rp2.FindAllStringSubmatch(html1, -1)       //成交额       pattern3 := `<td class="total">(.*?)</td>`       rp3 := regexp.MustCompile(pattern3)       find_txt3 := rp3.FindAllStringSubmatch(html1, -1)       //综合利率       pattern4 := `<td class="rate">(.*?)</td>`       rp4 := regexp.MustCompile(pattern4)       find_txt4 := rp4.FindAllStringSubmatch(html1, -1)       //投资人       pattern5 := `<td class="pnum">(.*?)</td>`       rp5 := regexp.MustCompile(pattern5)       find_txt5 := rp5.FindAllStringSubmatch(html1, -1)       //借款周期       pattern6 := `<td class="cycle">(.*?)</td>`       rp6 := regexp.MustCompile(pattern6)       find_txt6 := rp6.FindAllStringSubmatch(html1, -1)       //借款人       pattern7 := `<td class="p1num">(.*?)</td>`       rp7 := regexp.MustCompile(pattern7)       find_txt7 := rp7.FindAllStringSubmatch(html1, -1)       //满标速度       pattern8 := `<td class="fuload">(.*?)</td>`       rp8 := regexp.MustCompile(pattern8)       find_txt8 := rp8.FindAllStringSubmatch(html1, -1)       //累计贷款余额       pattern9 := `<td class="alltotal">(.*?)</td>`       rp9 := regexp.MustCompile(pattern9)       find_txt9 := rp9.FindAllStringSubmatch(html1, -1)       //资金净流入       pattern10 := `<td class="capital">(.*?)</td>`       rp10 := regexp.MustCompile(pattern10)       find_txt10 := rp10.FindAllStringSubmatch(html1, -1)       //// 写入UTF-8 BOM       f.WriteString("\xEF\xBB\xBF")       ////   打印全部数据和写入excel文件       for i := 0; i < len(find_txt1); i++ {              fmt.Printf("%s  %s  %s  %s  %s  %s  %s  %s  %s  %s\n",find_txt1[i][1],find_txt2[i][1],find_txt3[i][1],find_txt4[i][1],find_txt5[i][1],find_txt6[i][1],                     find_txt7[i][1],find_txt8[i][1],find_txt9[i][1],find_txt10[i][1])              f.WriteString(find_txt1[i][1] + "\t" + find_txt2[i][1] + "\t" + find_txt3[i][1] + "\t"+find_txt4[i][1] + "\t" + find_txt5[i][1] + "\t"+              find_txt6[i][1] + "\t"+find_txt7[i][1] + "\t" + find_txt8[i][1] + "\t" + find_txt9[i][1] + "\t"+find_txt10[i][1] + "\r\n")       }}func main() {       t1 := time.Now() // get current time       parse1()       elapsed := time.Since(t1)       fmt.Println("爬虫结束,总共耗时: ", elapsed)}

这里写图片描述

这里写图片描述

原创粉丝点击