# GoScrapySimulation
[![Release latest](https://img.shields.io/badge/Release-latest-blue.svg?style=flat-square)](https://github.com/allwaysLove/GoScrapySimulation/releases) [![MIT License](https://img.shields.io/badge/LICENSE-MIT-yellow.svg?style=flat-square)](https://github.com/allwaysLove/GoScrapySimulation/main/LICENSE)
## 概述
**[GoScrapySimulation](https://github.com/allwaysLove/GoScrapySimulation)** 基于 Go 语言编写的类似于 Python 的 Scrapy 框架的开源网络爬虫框架
## 安装
```shell
go get github.com/allwaysLove/GoScrapySimulation
```
## 使用示例
```go
package main
import (
"GoScrapySimulation"
"GoScrapySimulation/engine"
"github.com/antchfx/htmlquery"
"strings"
)
// GithubProjectInfo 声明数据的结构
type GithubProjectInfo struct {
star string
fork string
}
// ParseFunc 编写请求到网页内容后的解析方法,通过 ItemDataChannel 将 数据传输给 Pipeline 进行输出
func ParseFunc(content []byte, UrlItemChannel chan GoScrapySimulation.RequestItem, ItemDataChannel chan interface{}) {
docParser, err := htmlquery.Parse(strings.NewReader(string(content)))
if err != nil {
return
}
staredCount := htmlquery.FindOne(docParser, `//a[contains(@aria-label, "starred")]`)
forkedCount := htmlquery.FindOne(docParser, `//a[contains(@aria-label, "forked")]`)
ItemDataChannel <- GithubProjectInfo{
star: strings.TrimSpace(htmlquery.InnerText(staredCount)),
fork: strings.TrimSpace(htmlquery.InnerText(forkedCount)),
}
}
func main() {
// 将需要请求的 URL 以及其他参数 构造成 RequestItem,并存储到 RequestItem 类型的切片中
requestItemList := []GoScrapySimulation.RequestItem{{
Url: "https://github.com/allwaysLove/GoScrapySimulation",
Parser: ParseFunc,
}, {
Url: "https://github.com/allwaysLove/SchoolAssignmentManageSystem",
Method: "Get",
Parser: ParseFunc,
}}
// 创建一个爬虫引擎,并传入配置
MyEngine := engine.NewEngine(
// 起始 RequestItem 列表
engine.SetStartRequestItemList(requestItemList),
// 设定最大的并发数量
engine.SetConcurrentCount(200),
// 设定超时时间,当超过设定时间,仍没有任何新任务出现时,便会结束爬虫
engine.SetQuiteSpiderTimeout(5),
)
// 启动爬虫引擎
MyEngine.StartCrawler()
}
```
输出结果为
```shell
2021/11/14 01:35:40 Engine Config {
ConcurrentCount: 200
Pipeline: *GoScrapySimulation.DefaultPipeline, Memory Address: 0xc000050cd0
requestItemList: [
1. Url: https://github.com/allwaysLove/GoScrapySimulation, ParserFunc: main.ParseFunc
2. Url: https://github.com/allwaysLove/SchoolAssignmentManageSystem, ParserFunc: main.ParseFunc
]
}
2021/11/14 01:35:40 Spider Running ...
2021/11/14 01:35:41 Fetch Request Url: https://github.com/allwaysLove/GoScrapySimulation
2021/11/14 01:35:41 ProcessItem: {1 0}
2021/11/14 01:35:42 Fetch Request Url: https://github.com/allwaysLove/SchoolAssignmentManageSystem
2021/11/14 01:35:42 ProcessItem: {8 2}
2021/11/14 01:35:47 Info Of Engine {
TotalRequestCount: 2
TotalRequestErrorCount: 0
StartTime: 2021-11-14 01:35:40
EndTime: 2021-11-14 01:35:47
SpiderTimeConsuming: 1.997866s
TotalTimeConsuming: 6.997866s
}
2021/11/14 01:35:47 Timeout 5s, exit the crawler
```
这个示例中,使用的是框架默认的 Pipeline(DefaultPipeline),这个 Pipeline 在接收到 Item 数据时,就会将数据输出的控制台中,即上述输出结果的 `ProcessItem`。但这只是简单的将结构体输出出来,如果希望获取到更详细的信息,有两种方法:
1. 实现自定义数据结构体的 String 方法
```go
func (info GithubProjectInfo) String() string {
return fmt.Sprintf("star: %s , fork: %s", info.star, info.fork)
}
```
2. 定义自己的 Pipeline
```go
// 自定义 Pipeline 结构体
type MyPipeline struct {
GoScrapySimulation.DefaultPipeline
}
// 编写对每条数据的处理方法
func (defaultPipeline *MyPipeline) ProcessItemFunc(parameter interface{}, dataItem interface{}) interface{} {
info := dataItem.(GithubProjectInfo)
fmt.Printf("name: %s, star: %s, fork: %s\n", info.name, info.star, info.fork)
return nil
}
```
此时对每条数据的输出格式就变成了
```shell
2021/11/14 01:53:33 ProcessItem: name: GoScrapySimulation , star: 1 , fork: 0
2021/11/14 01:53:34 ProcessItem: name: SchoolAssignmentManageSystem , star: 8 , fork: 22021/11/14 01:53:33 Fetch Request Url: https://github.com/allwaysLove/GoScrapySimulation
```
需要注意的是,自定义 Pipeline 需要实现 `GoScrapySimulation。Pipeline` 接口.该接口包含三个方法:`StartPipeline`, `ProcessItemFunc`, `ClosePipeline`,具体写法详见文件 `pipeline.go` 中 DefaultPipeline 的实现
而本例中因不需要在 Pipeline 启动和关闭时进行操作,故为了简化操作,直接继承了默认的 `DefaultPipeline`,而在实际项目中,可以独自实现接口而无需继承。
示例代码详见 example 目录
没有合适的资源?快使用搜索试试~ 我知道了~
基于 Go 语言编写的类似于 Python 的 Scrapy 框架的开源网络爬虫框架。
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共10个文件
go:5个
md:2个
mod:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
需积分: 5 0 下载量 153 浏览量
2024-05-23
15:53:47
上传
评论
收藏 10KB ZIP 举报
温馨提示
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![3G】](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 10 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/d7864c98224046ec9a7f43fa6207c84e_qq_51320133.jpg!1)
生瓜蛋子
- 粉丝: 3882
- 资源: 6141
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)