Code Monkey home page Code Monkey logo

gorequests's Introduction

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())
}

gorequests's People

Contributors

sixgad avatar gamegrd avatar

Watchers

 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.