// 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队列的爬虫的更多相关文章

  1. swift语言之多线程操作和操作队列(下)———坚持51天吃掉大象(写技术文章)

    欢迎有兴趣的朋友,参与我的美女同事发起的活动<51天吃掉大象>,该美女真的很疯狂,希望和大家一起坚持51天做一件事情,我加入这个队伍,希望坚持51天每天写一篇技术文章.关注她的微信公众号: ...

  2. Redis和消息队列使用实战

    消息队列是在乐视这边非常普遍使用的技术.在我们部门内部,不同的项目使用的消息队列实现也不一样.下面是支付系统的流转图(部门兄弟画的,借用一下): 从图中可以看到,里面用到了kafka消息队列.作用是做 ...

  3. berkeley db储存URL队列的简单实现增、删、查

     Berkeley DB(BDB)是一个高效的嵌入式数据库编程库,C语言.C++.Java.Perl.Python.Tcl以及其它非常多语言都有其相应的API. Berkeley DB能够保存随意 ...

  4. Redis 做消息队列

    一般来说,消息队列有两种场景,一种是发布者订阅者模式,一种是生产者消费者模式.利用redis这两种场景的消息队列都能够实现.定义: 生产者消费者模式:生产者生产消息放到队列里,多个消费者同时监听队列, ...

  5. Redis作为消息队列服务场景应用案例

    NoSQL初探之人人都爱Redis:(3)使用Redis作为消息队列服务场景应用案例   一.消息队列场景简介 “消息”是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更 ...

  6. 怎样让Oracle支持中文? 语言_地域.字符集

    暂时不涉及数据库业务,但是今天入库的时候中文入库报错,考虑可能是字体不支持,留待备用. 来源:Linux社区  作者:robertkun 语言_地域.字符集SIMPLIFIED CHINESE_CHI ...

  7. redis resque消息队列

    Resque 目前正在学习使用resque .resque-scheduler来发布异步任务和定时任务,为了方便以后查阅,所以记录一下. resque和resque-scheduler其优点在于功能比 ...

  8. c语言_常见图片格式判断

    c语言_常见图片格式判断 我想尽各种思路.今天,终于把图片判断搞定了. 在此,我写一下我的思路.希望对那些不想看代码的朋友们有帮助. 常风的的图片格式有:bmp,png,jpg,gif等图片格式. 我 ...

  9. Redis打造URL缩短服务

    用Redis打造URL缩短服务   此文章的英文版本已首发于 CodeProject : Building a simple URL shorten service with Redis 阅读文章之前 ...

随机推荐

  1. Oracle Flashback(flashback table或drop)

    在Oracle 10g中,Flash back家族分为以下成员:Flashback DatabaseFlashback DropFlashback TableFlashback Query(分Flas ...

  2. Leetcode27--->Remove Element(移除数组中给定元素)

    题目:给定一个数组array和一个值value,移除掉数组中所有与value值相等的元素,返回新的数组的长度:要求:不能分配额外的数组空间,且必须使用原地排序的思想,空间复杂度O(1); 举例: Gi ...

  3. Balanced Lineup(ST)

    描述 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. On ...

  4. Jenkins+GitHub+maven

    介绍: http://192.168.193.128:8115/jenkins/ 用户名:admin密码:admin电子邮箱:admin@admin.com root/12345678添加新用户: t ...

  5. Swift 3:新的访问控制fileprivate和open

    在swift 3中新增加了两种访问控制权限 fileprivate和 open.下面将对这两种新增访问控制做详细介绍. fileprivate 在原有的swift中的 private其实并不是真正的私 ...

  6. iOS学习笔记32-iCloud入门

    一.iCloud云服务 iCloud是苹果提供的云端服务,用户可以将通讯录.备忘录.邮件.照片.音乐.视频等备份到云服务器并在各个苹果设备间直接进行共享而无需关心数据同步问题,甚至即使你的设备丢失后在 ...

  7. Key-value数据库:Redis缓存服务

    Redis 是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.其提供了多种主流语言的客户端,方便使用:同时Redis支持主 ...

  8. Dancing Links 模板

    struct dl{ // x: line, y: column struct node{ int c, left, right, up, down; }; vector<node> a; ...

  9. svg优雅降级技术

    这是一个名叫Alexey Ten首先提出来的,类似下面的代码: <svg width="96" height="96"> <image xli ...

  10. Spring Boot 配置大全

    Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. SpringBoot的配置方式有很多,它们的优先级如下所示(优 ...