go语言post提交数据

POST提交姓、名字,进行姓名评分


package craw

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"github.com/axgle/mahonia"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
)

func HttpPost(xing,ming string) string {

	enc:=mahonia.NewEncoder("gbk")
	xing =enc.ConvertString(xing)
	ming =enc.ConvertString(ming)
	
	resp, err := http.Post("https://m.sanzang5.com/xingming_ceshi_1.php",
		"application/x-www-form-urlencoded",
		strings.NewReader(fmt.Sprintf("ceshi_xing=%s&ceshi_ming=%s&dafen=%D0%D5%C3%FB%B2%E2%CA%D4%B4%F2%B7%D6",xing,ming)))
	if err != nil {
		log.Println(err)
	}
	resp.Header.Add("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1") //模拟浏览器User-Agent
	resp.Header.Set("accept-language", "zh-CN,zh;q=0.9")

	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println(err)
	}

	return ConvertToString(string(body), "GBK", "UTF-8")

}

func ConvertToString(src string, srcCode string, tagCode string) string {
	srcCoder := mahonia.NewDecoder(srcCode)
	srcResult := srcCoder.ConvertString(src)
	tagCoder := mahonia.NewDecoder(tagCode)
	_, cdata, _ := tagCoder.Translate([]byte(srcResult), true)
	result := string(cdata)
	return result
}


func GetReuslt (src string)string{

	dom, err := goquery.NewDocumentFromReader(strings.NewReader(src))
	if err != nil {
		log.Fatalln(err)
	}
	var result string
	dom.Find(".sanzang_abs").Each(func(i int, selection *goquery.Selection) {

		s1:=selection.Text()
		s1= strings.Replace(s1,"\t","",-1)
		//s1= strings.Replace(s1,"【","\n\n【",-1)
		s1= strings.Replace(s1,"\n\n","",-1)
		s1 =strings.Replace(s1,"[ 满分100分,60分及格 ] ","",-1)
		result =s1

	})
	return  result
}




你可能感兴趣的:(go,go)