【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)}
阅读全文
1 0
- 【go语言爬虫】网贷天眼数据平台爬虫
- 【R语言爬虫】网贷天眼平台表格数据爬虫1
- 【python爬虫】网贷天眼平台表格数据抓取
- 【R语言爬虫】网贷天眼数据平台表格数据抓取2
- 爬虫-天眼查数据
- Python爬虫---爬取天眼查数据(上)
- 拍拍贷 爬虫
- Go语言 简单的爬虫示例(1)
- go语言实现爬虫采集联想词
- go语言--爬虫--对字符串的处理
- 【go语言爬虫】go语言爬取豆瓣电影top250
- 大数据平台变“天眼”
- Python爬虫---爬取天眼查数据(下)tkinter和exe打包
- 工商企业数据-企信宝-天眼查-企查查360度深度分布式爬虫技术破解
- 网页数据爬虫-R语言
- 互联网金融爬虫怎么写-第一课 p2p网贷爬虫(XPath入门)
- go练习:Web 爬虫
- go 简单爬虫
- JavaScript中的变量
- csdn的资源怎么样
- Java Filter过滤机制详解
- 了解使用 ConstraintLayout
- Json简介与转换数据例子
- 【go语言爬虫】网贷天眼数据平台爬虫
- poj 2478
- 查询mysql 中某字段为空值的数据
- 分布式存储系统
- Django 中The 'image' attribute has no file associated with it.出错问题的解决
- Eclipse代码补全 空格键 "=" 键不上屏设置
- Java内存模型与线程02:主内存和工作内存
- opencv笔记
- 网易之小易最近在数学课上学习到了集合的概念,集合有三个特征:1.确定性 2.互异性 3.无序性.需要根据给定的w,x,y,z,求出集合中一共有多少个元素。