Go查询快递网数据
顺丰快递查询不了,其他快递亲测可用
package kuaidi
import (
"encoding/json"
"fmt"
"gintest/utils"
"time"
)
type Data struct {
Time string `json:"time"`
Context string `json:"context"`
}
type KuaiDi struct {
CompanyType string `json:"companytype"`
Nu string `json:"nu"`
Company string `json:"company"`
Data []Data `json:"data"`
}
/**
* 快递接口查询
* expressNo 快递单号
* com 快递公司
*/
func Query(expressNo, com string) KuaiDi {
url := fmt.Sprintf("http://www.kuaidi.com/index-ajaxselectcourierinfo-%s-%s-KUAIDI%s.html", expressNo, com, string(time.Now().Unix()))
header := make(map[string]string)
header["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
header["Referer"] = "http://www.kuaidi.com"
header["X-Requested-With"] = "XMLHttpRequest"
header["Content-Type"] = "application/json"
result := utils.Post(url, nil, header)
var kd KuaiDi
json.Unmarshal([]byte(result), &kd)
return kd
}
package utils
import (
"bytes"
"encoding/json"
"github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"time"
)
// 发送POST请求
// url: 请求地址
// data: POST请求提交的数据
// headers: 请求头内容
// result: 返回的内容
func Post(url string, data interface{}, headers interface{}) string {
client := &http.Client{Timeout: 5 * time.Second} // 超时时间:5秒
jsonStr, _ := json.Marshal(data)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
if err != nil {
logrus.Error(err)
}
if maps, ok := headers.(map[string]string); ok {
for k, v := range maps {
req.Header.Set(k, v)
}
}
resp, err := client.Do(req)
if err != nil {
logrus.Error(err)
}
defer resp.Body.Close()
result, _ := ioutil.ReadAll(resp.Body)
return string(result)
}
测试
str := kuaidi.Query("773097747419120", "shentong")
c.JSON(200, str)
返回json数值
{
"companytype": "shentong",
"nu": "773097747419120",
"company": "申通快递",
"data": [
{
"time": "2021-05-18 17:57:08",
"context": "客户签收:已签收。快件已从【焦村一中东边第二家门店申通快递】取出,签收人:【本人签收】。。有问题请联系【18839885051】"
},
{
"time": "2021-05-18 17:38:35",
"context": "快件已被【焦村镇共配站S】【焦村一中东边第二家门店申通快递】站点代收,请及时取件。有问题请联系【18839885051】"
},
{
"time": "2021-05-18 17:38:05",
"context": "【焦村镇共配站S】小件员【罗灵花】正在为您派件,如有疑问请联系小件员,联系电话【18839885051】"
},
{
"time": "2021-05-18 17:37:50",
"context": "快件已到达【河南灵宝焦村营业部】扫描员是【焦村镇自取件】"
},
{
"time": "2021-05-18 17:37:35",
"context": "快件由【河南灵宝公司】发往【河南灵宝焦村营业部】"
},
{
"time": "2021-05-18 08:57:08",
"context": "快件已到达【河南灵宝公司】扫描员是【张冬梅】"
},
{
"time": "2021-05-18 02:29:59",
"context": "快件由【河南洛阳转运中心】发往【河南灵宝公司】"
},
{
"time": "2021-05-18 02:29:30",
"context": "快件由【河南洛阳转运中心】发往【河南灵宝公司】"
},
{
"time": "2021-05-18 02:23:11",
"context": "快件已到达【河南洛阳转运中心】扫描员是【刘鹏辉】"
},
{
"time": "2021-05-18 02:21:14",
"context": "快件已到达【河南洛阳转运中心】扫描员是【新东进港】"
},
{
"time": "2021-05-17 17:16:16",
"context": "快件由【河南郑州转运中心】发往【河南洛阳转运中心】"
},
{
"time": "2021-05-17 17:09:29",
"context": "快件已到达【河南郑州转运中心】扫描员是【AX王木娥】"
},
{
"time": "2021-05-16 22:47:08",
"context": "快件由【山东济南转运中心】发往【河南郑州转运中心】"
},
{
"time": "2021-05-16 22:38:35",
"context": "快件已到达【山东济南转运中心】扫描员是【郑州出港】"
},
{
"time": "2021-05-16 17:11:28",
"context": "快件由【山东省市场部二十部】发往【山东济南转运中心】"
},
{
"time": "2021-05-16 15:57:12",
"context": "【山东省市场部二十部】的收件员【派得】已收件"
}
]
}