Scraping Framework for Golang http://go-colly.org/

https://github.com/gocolly/colly

package main

import (
"fmt" "github.com/gocolly/colly"
"time"
"regexp"
"strings"
) /*
task
http://www.cnhan.com/hyzx/
http://www.cnhan.com/shantui/
http://www.cnhan.com/pinfo/ http://www.heze.cn/info
http://www.heze.cn/qiye/ 采集站点当日更新数据的客户联系方式 */
func getTodayUrls() []string {
var todayUrls []string
// Instantiate default collector
c := colly.NewCollector(
colly.AllowedDomains("www.cnhan.com"),
)
// On every a element which has href attribute call callback
// 类选择器
//url仅在本页
c.OnHTML(".showSort a[href]", func(e *colly.HTMLElement) {
link := e.Attr("href")
todayUrls = append(todayUrls, link)
fmt.Printf("Link found: %q -> %s\n", e.Text, link)
}) // Start scraping on http://www.cnhan.com/shantui/
c.Visit("http://www.cnhan.com/shantui/") //起始路由改变
// Instantiate default collector
c = colly.NewCollector(
colly.AllowedDomains("www.cnhan.com"),
colly.URLFilters(
//请求页面的正则表达式,满足其一即可
//http://www.cnhan.com/hyzx/
//http://www.cnhan.com/hyzx/index-all-2.html
//硬代码:当天最多更新99页http://www.cnhan.com/hyzx/index-all-99.html
//^[1-9][0-9]{0,1}[^0-9]{0,1}$
regexp.MustCompile("^http://www.cnhan.com/hyzx/(.{0}$)|(index-all-[1-9][0-9]{0,1}[^0-9]{0,1}\\.html$)"),
),
)
// On every a element which has href attribute call callback
c.OnHTML("a[href]", func(e *colly.HTMLElement) {
link := e.Attr("href")
fmt.Printf("Link found: %q -> %s\n", e.Text, link)
c.Visit(e.Request.AbsoluteURL(link))
datetime := time.Now().Format("20060102")
fmt.Println(datetime)
reg := regexp.MustCompile(datetime) // http://www.cnhan.com/hyzx/20180827/7109076.html 通过url格式过滤出今天的url
data := reg.Find([]byte(link))
regRes := len(data)
if regRes > 0 {
link = "http://www.cnhan.com/hyzx/" + link
todayUrls = append(todayUrls, link)
}
}) // Before making a request print "Visiting ..."
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL.String())
}) // Start scraping on http://www.cnhan.com/shantui/
c.Visit("http://www.cnhan.com/hyzx/") //起始路由改变
// Instantiate default collector
c = colly.NewCollector(
colly.AllowedDomains("www.cnhan.com"),
colly.URLFilters(
//请求页面的正则表达式,满足其一即可
//http://www.cnhan.com/pinfo/
//http://www.cnhan.com/pinfo/index-5.html
//硬代码:当天最多更新99页http://www.cnhan.com/pinfo/index-99.html
regexp.MustCompile("^http://www.cnhan.com/pinfo/(.{0}$)|(index-[1-9][0-9]{0,1}[^0-9]{0,1}\\.html$)"),
),
)
// On every a element which has href attribute call callback
c.OnHTML("a[href]", func(e *colly.HTMLElement) {
link := e.Attr("href")
fmt.Printf("Link found: %q -> %s\n", e.Text, link)
c.Visit(e.Request.AbsoluteURL(link))
//文本过滤
eDate := e.ChildText(".span2")
//http://www.cnhan.com/pinfo/313257.html 周口水泥彩砖具有的特色是什么2018.08.27
datetime := time.Now().Format("2006.01.02")
if (strings.Contains(eDate, datetime)) {
link := e.Attr("href")
link = "http://www.cnhan.com" + link
fmt.Printf("Link found: %q -> %s\n", e.Text, link)
todayUrls = append(todayUrls, link)
}
}) // Before making a request print "Visiting ..."
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL.String())
}) // Start scraping on http://www.cnhan.com/shantui/
c.Visit("http://www.cnhan.com/pinfo/") //起始路由改变
// Instantiate default collector
c = colly.NewCollector(
colly.AllowedDomains("www.heze.cn"),
)
// On every a element which has href attribute call callback
// 类选择器
c.OnHTML(".news_list_r a[href]", func(e *colly.HTMLElement) {
link := e.Attr("href")
fmt.Printf("Link found: %q -> %s\n", e.Text, link)
todayUrls = append(todayUrls, link)
}) // Before making a request print "Visiting ..."
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL.String())
}) // Start scraping on http://www.cnhan.com/shantui/
c.Visit("http://www.heze.cn/info/") /*
站内目标url
http://www.heze.cn/info/
http://www.heze.cn/qiye/
检测思路:
1、按父url,分别进入 http://www.heze.cn/qiye/18240670888/show-37-1367148.html http://www.heze.cn/info/LEbigong/show-1-13931879.html
与2反
2、按照全站进入
优点:过滤规则简单,代码代码简单;爬取结果数据不便于分类处理,比如产品类型、发布时间;
缺点:爬爬取速度慢
*/ //起始路由改变
//http://www.heze.cn/qiye/ 该页面、其主体子页面,刷新,内容变化
//http://www.heze.cn/qiye/list-8.html
// Instantiate default collector
c = colly.NewCollector(
colly.AllowedDomains("www.heze.cn"),
colly.URLFilters(
//请求页面的正则表达式,满足其一即可
regexp.MustCompile("^http://www.heze.cn/qiye/(.{0}$)|(list-\\d+-\\d+\\.html$)"),
),
)
// On every a element which has href attribute call callback
c.OnHTML("a[href]", func(e *colly.HTMLElement) {
link := e.Attr("href")
fmt.Printf("Link found: %q -> %s\n", e.Text, link)
c.Visit(e.Request.AbsoluteURL(link))
// http://www.heze.cn/qiye/hongfei688/show-44-14825619.html
reg := regexp.MustCompile("^http://www.heze.cn/qiye/[0-9a-zA-Z]+/show-\\d+-\\d+\\.html$")
data := reg.Find([]byte(link))
regRes := len(data)
if regRes > 0 {
fmt.Printf("Link found: %q -> %s\n", e.Text, link)
todayUrls = append(todayUrls, link)
}
}) // Before making a request print "Visiting ..."
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL.String())
}) // Start scraping on http://www.heze.cn/qiye/
c.Visit("http://www.heze.cn/qiye/") return todayUrls
} func main() {
var todayUrls = getTodayUrls()
fmt.Println(todayUrls)
fmt.Println(len(todayUrls))
}

Colly provides a clean interface to write any kind of crawler/scraper/spider的更多相关文章

  1. 使用composer安装php的相关框架

    使用composer来安装php的相关框架,不需要事先准备composer.json以及conmposer.lock以及composer.phar等文件: 直接在项目根目录下是使用composer r ...

  2. Your First ASP.NET 5 Application on a Mac

    Your First ASP.NET 5 Application on a Mac By Daniel Roth, Steve Smith, Rick Anderson ASP.NET 5 is cr ...

  3. Scott Hanselman's 2014 Ultimate Developer and Power Users Tool List for Windows -摘自网络

    Everyone collects utilities, and most folks have a list of a few that they feel are indispensable.  ...

  4. 【翻译】在Mac上使用VSCode创建你的第一个Asp.Net Core应用

    Setting Up Your Development Environment 设置你的开发环境 To setup your development machine download and inst ...

  5. 开源蜘蛛集合(转自haizhiguang博客,链接:http://blog.csdn.net/haizhiguang/article/details/20209573)

    各种蜘蛛: Heritrix   点击次数:1458 Heritrix是一个开源,可扩展的web爬虫项目.Heritrix设计成严格按照robots.txt文件的排除指示和META robots标签. ...

  6. Less is exponentially more

    Less is exponentially more  (原文出处:rob pike 博客,https://commandcenter.blogspot.jp/2012/06/less-is-expo ...

  7. [转] h5上传视频或文件编写

    Html5 finally solves an age old problem of being able to upload files while also showing the upload ...

  8. getting-started-with-mqtt

    来自:https://dzone.com/refcardz/getting-started-with-mqtt SECTION 1 Why MQTT? The Internet of Things ( ...

  9. csredis base usage

    Basic usage Whenever possible, server responses are mapped to the appropriate CLR type. using (var r ...

随机推荐

  1. Object转json-常见问题总结

    Object转json-常见问题总结 1.Object中有Timestamp.Date等日期类型数据 http://blog.csdn.net/without0815/article/details/ ...

  2. udp 多播

    先来了解下UDP UDP 是UserDatagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interconnection,开放式系统互联) 参考模型中一 ...

  3. UPC 2223: A-Number and B-Number(数位DP+二分)

    积累点: 1: (l&r)+((l^r)>>) == (l+r)/2 2: 注意判断现在是否有限制.当枚举下一个量时,是(isQuery && j==end),不要 ...

  4. HDU 5790 Prefix(Hash + 主席树)

    题目链接  Prefix 题意  给定一个字符串序列,求第$l$个字符串到第$r$个字符串之间有多少个不同的前缀 强制在线 考虑$Hash$ 首先把所有前缀都$hash$出来,按顺序组成一个长度不超过 ...

  5. POJ 3140 Contestants Division (树dp)

    题目链接:http://poj.org/problem?id=3140 题意: 给你一棵树,问你删去一条边,形成的两棵子树的节点权值之差最小是多少. 思路: dfs #include <iost ...

  6. "visual studio 2012 安装引擎拒绝访问" 错误的解决

    首先,我们看一下错误的具体提示,如下图所示: 这个错误,是我安装了那么多年Visual Studio的经历中,第一次遇到.太恶心了,昨天一直安装失败,导致后续其它软件的安装一再拖延.目前网上的解决方案 ...

  7. 分布式文件系统 FastDFS 5.0.8 & Linux CentOS 6.7 安装配置

    原文:http://blog.csdn.net/wlwlwlwl015/article/details/52619851 前言 项目中用到文件服务器,有朋友推荐用fastdfs,所以就了解学习了一番, ...

  8. NSThread学习

    使用多线程可以防止主线程阻塞.同时也可以将一个大的任务分成若干个小的任务去做. 常用方法一: 1, 首先使用  detachNewThreadSelector:toTarget:withObject: ...

  9. UITableView 滚动时使用reloaddata出现 crash'-[__NSCFArray objectAtIndex:]: index (1) beyond bounds (0)' Crash

    例子: - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)in ...

  10. 拦截器及 Spring MVC 整合

    一.实验介绍 1.1 实验内容 本节课程主要利用 Spring MVC 框架实现拦截器以及 Spring MVC 框架的整合. 1.2 实验知识点 Spring MVC 框架 拦截器 1.3 实验环境 ...