gorequests

package module
v0.0.0-...-8e5ee2d Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2023 License: MIT Imports: 14 Imported by: 0

README

gorequests

像python一样优雅的Go爬虫库

通过对net/http的再一次封装,使得gorequests的语法更接近python requests的写法

一、安装

go get github.com/gamegrd/gorequests

二、Demo

1.get请求

package main

import (
	"fmt"

	"github.com/gamegrd/gorequests"
)

func main() {
	resp, err := gorequests.Get("https://www.httpbin.org/get?a=1&b=2")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(resp.Text())

	// Params
	params := gorequests.Params{"a": "1", "b": "2"}
	res, err := gorequests.Get("https://www.httpbin.org/get", params)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(res.Text())
}

2.post请求

package main

import (
	"fmt"

	"github.com/gamegrd/gorequests"
)

func main() {
	// form post
	data := gorequests.Data{
		"name": "xiaobulv",
	}
	resp, _ := gorequests.Post("https://www.httpbin.org/post", data)
	fmt.Println(resp.Text())

	// json post
	jdata := gorequests.Json{"AGE": "131"}
	respb, _ := gorequests.Post("https://www.httpbin.org/post", jdata)
	fmt.Println(respb.Text())

}

3.put delete patch等

package main

import (
	"fmt"

	"github.com/gamegrd/gorequests"
)

func main() {
	// put
	putdata := gorequests.Json{"AGE": "131"}
	respp, _ := gorequests.Put("https://www.httpbin.org/put", putdata)
	fmt.Println(respp.Text())
}

4.auth

package main

import (
	"github.com/gamegrd/gorequests"
)

func main() {
	respa, _ := gorequests.Get("https://www.httpbin.org/basic-auth/admin/123456", gorequests.Auth{"admin", "123456"})
	println(respa.Text())
}

5.header, cookie, timeout, proxy, json等设置

package main

import (
	"fmt"

	"github.com/gamegrd/gorequests"
)

func main() {
	// 设置header
	header := gorequests.Header{"user-agent": "hello"}

	// post 请求参数
	data := gorequests.Data{
		"name": "xiaobulv",
	}

	// 设置cookie
	// 当Header里有Cookie时, 此设置无效
	ck := gorequests.Cookie{"BIDUPSID": "C855441CA6145FBB2741293580"}

	// timeout
	timeout := gorequests.SetTimeout(10)

	// 代理
	// proxy := gorequests.Proxy("http://xxx.ip.com")

	resp, _ := gorequests.Post("https://www.httpbin.org/post", data, header, ck, timeout)

	// content
	fmt.Println(resp.Content())

	// text
	fmt.Println(resp.Text())

	// response.Json
	m := make(map[string]interface{})
	resp.Json(&m)
	fmt.Println(m["headers"])
	c := m["headers"]
	fmt.Println(c.(map[string]interface{})["Host"])

	// 状态码
	fmt.Println(resp.StatusCode)

	// 响应cookie
	fmt.Println(resp.Cookies())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth

type Auth []string

{username,password}

type Cookie map[string]string

type Data

type Data map[string]string

type Files

type Files map[string]string // name ,filename
type Header map[string]string

type Json

type Json map[string]string

type Params

type Params map[string]string

type Proxy

type Proxy string

type Request

type Request struct {
	Header *http.Header
	Client *http.Client
	// contains filtered or unexported fields
}

func Requests

func Requests() *Request

func (*Request) Close

func (req *Request) Close()

func (*Request) ReqCookies

func (req *Request) ReqCookies() []*http.Cookie

func (*Request) Send

func (req *Request) Send(method string, origurl string, args ...interface{}) (resp *Response, err error)

type Response

type Response struct {
	Res *http.Response

	Req        *Request
	Status     string // e.g. "200 OK"
	StatusCode int    // e.g. 200
	Header     http.Header
	// contains filtered or unexported fields
}

func Delete

func Delete(origurl string, args ...interface{}) (resp *Response, err error)

func Get

func Get(origurl string, args ...interface{}) (resp *Response, err error)
func Head(origurl string, args ...interface{}) (resp *Response, err error)

常见几种请求类型

func Patch

func Patch(origurl string, args ...interface{}) (resp *Response, err error)

func Post

func Post(origurl string, args ...interface{}) (resp *Response, err error)

func Put

func Put(origurl string, args ...interface{}) (resp *Response, err error)

func (*Response) Content

func (resp *Response) Content() []byte

func (*Response) Cookies

func (resp *Response) Cookies() (cookies []*http.Cookie)

func (*Response) Json

func (resp *Response) Json(v interface{}) error

func (*Response) Location

func (resp *Response) Location() (*url.URL, error)

func (*Response) SaveFile

func (resp *Response) SaveFile(filename string) error

func (*Response) Text

func (resp *Response) Text() string

type SetTimeout

type SetTimeout int

Jump to

Keyboard shortcuts

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