GO语言_用redis作为url队列的爬虫
// Copyright 2016 laosj Author @songtianyi. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License. package main import (
"github.com/songtianyi/laosj/downloader"
"github.com/songtianyi/laosj/spider"
"github.com/songtianyi/rrframework/connector/redis"
"github.com/songtianyi/rrframework/logs"
"github.com/songtianyi/rrframework/storage"
"regexp"
"strconv"
"sync"
) func main() {
d := &downloader.Downloader{
ConcurrencyLimit: ,
UrlChannelFactor: ,
RedisConnStr: "127.0.0.1:6379",
SourceQueue: "DATA:IMAGE:MZITU:XINGGAN",
Store: rrstorage.CreateLocalDiskStorage("/Users/deer_mac/Desktop/自拍/"),
}
go func() {
d.Start()
}() // step1: find total pages
s, err := spider.CreateSpiderFromUrl("http://www.mzitu.com/share")
if err != nil {
logs.Error(err)
return
}
rs, _ := s.GetText("div.main>div.main-content>div.postlist>div>div.pagenavi-cm>a")
max := spider.FindMaxFromSliceString(, rs) // step2: for every page, find all img tags
var wg sync.WaitGroup
var mu sync.Mutex
step2 := make([]string, )
for i := ; i <= max; i++ {
wg.Add()
go func(ix int) {
defer wg.Done()
ns, err := spider.CreateSpiderFromUrl(s.Url + "/comment-page-" + strconv.Itoa(ix) + "#comments/")
if err != nil {
logs.Error(err)
return
}
t, _ := ns.GetHtml("div.main>div.main-content>div.postlist>div>ul>li>div>p")
mu.Lock()
step2 = append(step2, t...)
mu.Unlock()
}(i)
}
wg.Wait()
err, rc := rrredis.GetRedisClient(d.RedisConnStr)
if err != nil {
logs.Error(err)
return
}
// parse url
for _, v := range step2 {
re := regexp.MustCompile("src=\"(\\S+)\"")
url := re.FindStringSubmatch(v)[]
key := d.SourceQueue
if _, err := rc.RPush(key, url); err != nil {
logs.Error(err)
return
}
}
d.WaitCloser()
}
首先要开启redis服务, 然后就可以了.
GO语言_用redis作为url队列的爬虫的更多相关文章
- swift语言之多线程操作和操作队列(下)———坚持51天吃掉大象(写技术文章)
欢迎有兴趣的朋友,参与我的美女同事发起的活动<51天吃掉大象>,该美女真的很疯狂,希望和大家一起坚持51天做一件事情,我加入这个队伍,希望坚持51天每天写一篇技术文章.关注她的微信公众号: ...
- Redis和消息队列使用实战
消息队列是在乐视这边非常普遍使用的技术.在我们部门内部,不同的项目使用的消息队列实现也不一样.下面是支付系统的流转图(部门兄弟画的,借用一下): 从图中可以看到,里面用到了kafka消息队列.作用是做 ...
- berkeley db储存URL队列的简单实现增、删、查
Berkeley DB(BDB)是一个高效的嵌入式数据库编程库,C语言.C++.Java.Perl.Python.Tcl以及其它非常多语言都有其相应的API. Berkeley DB能够保存随意 ...
- Redis 做消息队列
一般来说,消息队列有两种场景,一种是发布者订阅者模式,一种是生产者消费者模式.利用redis这两种场景的消息队列都能够实现.定义: 生产者消费者模式:生产者生产消息放到队列里,多个消费者同时监听队列, ...
- Redis作为消息队列服务场景应用案例
NoSQL初探之人人都爱Redis:(3)使用Redis作为消息队列服务场景应用案例 一.消息队列场景简介 “消息”是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更 ...
- 怎样让Oracle支持中文? 语言_地域.字符集
暂时不涉及数据库业务,但是今天入库的时候中文入库报错,考虑可能是字体不支持,留待备用. 来源:Linux社区 作者:robertkun 语言_地域.字符集SIMPLIFIED CHINESE_CHI ...
- redis resque消息队列
Resque 目前正在学习使用resque .resque-scheduler来发布异步任务和定时任务,为了方便以后查阅,所以记录一下. resque和resque-scheduler其优点在于功能比 ...
- c语言_常见图片格式判断
c语言_常见图片格式判断 我想尽各种思路.今天,终于把图片判断搞定了. 在此,我写一下我的思路.希望对那些不想看代码的朋友们有帮助. 常风的的图片格式有:bmp,png,jpg,gif等图片格式. 我 ...
- Redis打造URL缩短服务
用Redis打造URL缩短服务 此文章的英文版本已首发于 CodeProject : Building a simple URL shorten service with Redis 阅读文章之前 ...
随机推荐
- Python之code对象与pyc文件(二)
上一节:Python之code对象与pyc文件(一) 创建pyc文件的具体过程 前面我们提到,Python在通过import或from xxx import xxx时会对module进行动态加载,如果 ...
- LayoutInflater的用法
Instantiates a layout XML file into its corresponding View objects. It is never used directly. Inste ...
- NetBeans无法使用编码GBK安全地打开该文件
今天用NetBeans打开包含路径里面的UTF-8编码的文件时,提示:NetBeans无法使用编码GBK安全地打开该文件. 后来在网上搜索找到了解决方案,原文地址:http://qdjinxin.it ...
- JDK并发基础与部分源码解读
之前写的一个ppt 搬到博客来
- [python篇][其他] python博客学习汇总
http://blog.csdn.net/zhangxinrun/article/details/8141913
- AtCoder Grand Contest 021
A - Digit Sum 2 Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Find t ...
- AtCoder Regular Contest 089
这场一边吃饭一边打,确实还是很菜的 C - Traveling Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem ...
- [luoguP3302] [SDOI2013]森林(主席树 + 启发式合并 + lca)
传送门 显然树上第k大直接主席树 如果连边的话,我们重构小的那一棵,连到另一棵上. 说起来简单,调了我一晚上. 总的来说3个错误: 1.离散化写错位置写到了后面 2."="写成了& ...
- 【前端学习笔记】2015-09-06 ~~~~ setAttribute()、slice()
所遇记录: 1.setAttribute("属性",value),相同的还有addAttribute("属性名",value),getAttribute(“属性 ...
- 集合工具类CollectionUtils、ListUtils、SetUtils、MapUtils探究(转)
之前一直以为集合工具类只有CollectionUtils,主要用它的isEmpty(final Collection<?> coll)静态方法来判断一个给定的集合是否为null或者是否长度 ...