qust

package module
v0.0.0-...-ed3f7c1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2023 License: MulanPSL-2.0 Imports: 20 Imported by: 0

README

Logo

Qust

7天从0实现的爬虫框架

> 听说Go语言是出了名的造轮子语言,打算新写个项目(造轮子),项目主旨就是简单,轻量,方便的爬虫。 >

下面是标准库进行简单的JSON爬取,不管是解析JSON,还是获取数据,都异常麻烦,需要写大量的错误判断,和解析代码,面对复杂的JSON更是头疼,需要一直反射还可能会遇到错误,这个框架就是为了简化这些操作,让代码简洁并且容错高,错误处理方便。

写法会偏向Pythonrequests库,毕竟学的第一语言就是Python,而且还是直接从requests爬虫入门的

func main() {
	url := "http://t.weather.sojson.com/api/weather/city/101210101"
	html, err := http.Get(url)
	if err != nil {
		fmt.Println(err)
	}
	defer html.Body.Close()
	jsonData, _ := io.ReadAll(html.Body)
	var v interface{}
	_ = json.Unmarshal(jsonData, &v)
	data := v.(map[string]interface{})
	fmt.Println(data["cityInfo"].(map[string]interface{})["city"], data["data"].(map[string]interface{})["forecast"])
}

Qust 框架

为了方便了解go的标准库,又不枯燥,所以打算慢慢的写一个项目入手,然后去看go的源码。算是一边学习,一边总结经验和写教程,主要受了极客兔兔大佬七天系列影响,看了Gee感觉很有意思,但模仿着写Gee又没提升,所以自己探索!

也会去参考大佬们的源码,尽量全部使用标准库

这个库的计划 >

  • 格式支持
    • Json
    • Html
    • File
    • Xml
    • Csv
  • 便捷性
    • 可以指定基础路径,减少后续的url长度
    • 可以进行分组,不同分组基础路径可以不同
    • 高并发
    • 模拟表单Form提交
  • 反爬
    • 代理池
    • 方便的添加headers
    • 随机生成ua
    • 可等待js渲染完在爬取html
    • ck缓存
    • 支持robots.txt

目录

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseUrl

type BaseUrl string

type DataType

type DataType string
const (
	JSON          DataType = "application/json"
	FROM          DataType = "multipart/form-data"
	XML           DataType = "text/xml"
	MultipartForm DataType = "application/x-www-form-urlencoded"
)

type Engine

type Engine struct {
	BaseUrl BaseUrl
	Client  *http.Client
	Version int
	Proxys  *Proxys
}

Engine qust主函数

func New

func New(args ...any) *Engine

New 创建Qust

args:
BaseUrl:基础路径,
http.Client:全局客户端,

func (*Engine) Ask

func (engine *Engine) Ask(method string, url string) *Req

func (*Engine) Delete

func (engine *Engine) Delete(url string) *Req

func (*Engine) Get

func (engine *Engine) Get(url string) *Req

func (*Engine) Patch

func (engine *Engine) Patch(url string) *Req

func (*Engine) Post

func (engine *Engine) Post(url string) *Req

func (*Engine) Put

func (engine *Engine) Put(url string) *Req

func (*Engine) SetProxy

func (engine *Engine) SetProxy(protocol string, address string) error

func (*Engine) SetProxys

func (engine *Engine) SetProxys(proxys []string, mode int) *Proxys

type Proxys

type Proxys struct {
	Proxy []string
	Index int
	Size  int
	Mode  int
	sync.Mutex
}

type Req

type Req struct {
	Url    *url.URL
	Method string
	Query  map[string]any
	Client *http.Client
	Body   io.Reader
	Header http.Header
	Proxy  string
}

func (*Req) AddHeader

func (req *Req) AddHeader(k any, args ...any) *Req

func (*Req) Do

func (req *Req) Do() (*Res, error)

func (*Req) SetCookie

func (req *Req) SetCookie(data any) *Req

func (*Req) SetData

func (req *Req) SetData(dtype DataType, datas ...any) (err error)

func (*Req) SetQuery

func (req *Req) SetQuery(k any, args ...any) *Req

func (*Req) SetUA

func (req *Req) SetUA(ua string) *Req

type Res

type Res struct {
	StatusCode int
	Proto      string
	Header     http.Header
	Body       []byte
}

Res 返回值类型

func (*Res) Encoding

func (res *Res) Encoding(args ...any) error

Encoding 进行编码

func (*Res) File

func (res *Res) File() *convert.FileData

File 获取File数据

func (*Res) Html

func (res *Res) Html() (*convert.HtmlData, error)

Html 获取Html数据

func (*Res) Json

func (res *Res) Json() (*convert.JsonData, error)

Json 获取Json数据

func (*Res) Text

func (res *Res) Text() string

Text 获取文本

func (*Res) Xml

func (res *Res) Xml()

Xml 获取Xml数据

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL