Colly provides a clean interface to write any kind of crawler/scraper/spider
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的更多相关文章
- 使用composer安装php的相关框架
使用composer来安装php的相关框架,不需要事先准备composer.json以及conmposer.lock以及composer.phar等文件: 直接在项目根目录下是使用composer r ...
- 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 ...
- 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. ...
- 【翻译】在Mac上使用VSCode创建你的第一个Asp.Net Core应用
Setting Up Your Development Environment 设置你的开发环境 To setup your development machine download and inst ...
- 开源蜘蛛集合(转自haizhiguang博客,链接:http://blog.csdn.net/haizhiguang/article/details/20209573)
各种蜘蛛: Heritrix 点击次数:1458 Heritrix是一个开源,可扩展的web爬虫项目.Heritrix设计成严格按照robots.txt文件的排除指示和META robots标签. ...
- Less is exponentially more
Less is exponentially more (原文出处:rob pike 博客,https://commandcenter.blogspot.jp/2012/06/less-is-expo ...
- [转] h5上传视频或文件编写
Html5 finally solves an age old problem of being able to upload files while also showing the upload ...
- getting-started-with-mqtt
来自:https://dzone.com/refcardz/getting-started-with-mqtt SECTION 1 Why MQTT? The Internet of Things ( ...
- csredis base usage
Basic usage Whenever possible, server responses are mapped to the appropriate CLR type. using (var r ...
随机推荐
- [leetcode] permutations 排列
写了两个,一个是直接的递归实现: class Solution { public: void swap(vector<int> &num,int left,int right) { ...
- windows8.1如何分盘
磁盘分区首先要弄明白磁盘物理顺序与逻辑顺序的区别,在[磁盘管理]界面,所显示的前后顺序为物理顺序,这是磁盘上实实在在的物理位置,如下图2的电脑磁盘物理顺序为CFDE.在[资源管理器]界面,所显示的顺序 ...
- 汇编指令详解--as手册
https://sourceware.org/binutils/docs/as/ Table of Contents 1 Overview 1.1 Structure of this Manual 1 ...
- PHP读取APK的包信息,包括包名,应用名,权限,LOGO等
[转]PHP读取APK的包信息,包括包名,应用名,权限,LOGO等 声明本文转自: 原文链接:https://www.jb51.net/article/53780.htm: 感谢分享! <?ph ...
- 表格 td中,取checkbox后几位值
function addToPanDianDetail() { var detail_id = ""; var detail_code = ""; $(&quo ...
- TStringList 善用 value['names'] 即使value 是带=号的值都没有关系呵呵 ,我靠 强,以后就用这个了,key=value首选
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA4YAAAHiCAIAAAA760U/AAAgAElEQVR4nOy9Z5QUV57oWe/j7tk9u2 ...
- win10 升级导致找不到SQL Server配置管理器
1.背景 SQL Server配置管理器可用来管理与SQL Server相关联的服务.配置SQL Server使用的网络协议以及从SQL Server客户端计算机管理网络连接配置.但是win10从17 ...
- Android 蓝牙 笔记
安卓:短信复制机制 软件识别蓝牙 软件可以读取蓝牙的信息 给蓝牙弄上一个类似于短信的东西 并且存一个短信 然后发到客户端 客户端 可以分类 分成表格 形式 这样做的目的是为了 让你的设备可以写蓝牙给 ...
- 【CSS】获取元素的z-index值以及各种值的意义
js可以获取其元素的z-index值: $("document").ready(function(){ var a = $('.row').css('z-index'); aler ...
- libsvm交叉验证与网格搜索(参数选择)
首先说交叉验证.交叉验证(Cross validation)是一种评估统计分析.机器学习算法对独立于训练数据的数据集的泛化能力(generalize), 能够避免过拟合问题.交叉验证一般要尽量满足:1 ...