Coder Social home page Coder Social logo

go-douban-spider's Introduction

go-douban-spider

简介

基于go的豆瓣电影多线程爬虫,爬取豆瓣电影与电视剧内容、下载相应资源。支持自定义持久化(mongodb、mysql)。基于goroutine轻量级协程与channel通信机制实现高效并发,代码量很少,适合对于golang还不太熟悉的小伙伴学习使用~

架构&思路

  • [预处理器]->解析url并添加可爬取超链到channel中->作为消费者

  • [蜘蛛任务]->得到爬取结果,解析可利用资源->存入channel->作为消费者,消费超链channel,同时作为生产者

  • [持久化引擎]->消费爬取结果,进行持久化->作为消费者

  • [下载器]->消费可利用资源,下载model中的图片资源->作为消费者

核心流程

// 1.预处理器->解析url->urls(chan)-生产者
// 2.蜘蛛任务->得到results-tv(chan)*2 -(消费者,消费urls)+(生产者)
// 3.持久化引擎->消费results1-tv(chan)->持久化->消费者
// 4.下载器->消费results2-tv(chan)->下载model中的图片资源->消费者
urls := make(chan string, 5000)
results := make(chan Result, 5000)
resources := make(chan Resource, 5000)

//1.启动下载器任务
downLoadTask := CreateDownLoadTask("./download", resources)
go downLoadTask.Start()

//2.启动持久化任务
persistenceTask := CreatePersistenceTask(CreateMonoPersistence(), results)
go persistenceTask.Start()

//3.启动蜘蛛任务
spiderTask := CreateSpiderTask(resources, results, urls)
go spiderTask.Start()

//4.启动预处理器任务
prepareTask := CreatePrepareTask(urls)
go prepareTask.Start()

构建&使用

1.gopath初始化

cd $GOPATH/src
mkdir -p github.com/PualrDwade
cd github.com/PualrDwade

2.获取项目

git clone https://github.com/PualrDwade/go-douban-spider.git
cd go-douban-spider

3.编译

make

4.运行

make run

5.测试

make test

6.清理

make clean

结果展示

todo

  • 提供更加健壮的运行时机制,减少panic错误
  • 加入随机User-Agent,减少handshake error
  • 提供代理池功能,提高反反爬虫能力
  • 抽取公共配置到yml文件中,提供定制化

go-douban-spider's People

Contributors

pualrdwade avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.