package main
import (
"fmt"
"log"
"net/http"
"github.com/antchfx/htmlquery"
"github.com/gin-gonic/gin"
)
func getvalue(pages string) []interface{} {
var data map[string]interface{}
var datalist []interface{}
url := fmt.Sprintf("https://hostloc.com/forum-45-%s.html", pages)
method := "GET"
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Println(err)
}
req.Header.Add("authority", "hostloc.com")
req.Header.Add("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
req.Header.Add("accept-language", "zh-CN,zh;q=0.9")
req.Header.Add("cache-control", "no-cache")
req.Header.Add("pragma", "no-cache")
req.Header.Add("referer", "https://hostloc.com/forum-45-1.html")
req.Header.Add("sec-ch-ua", "\"Chromium\";v=\"118\", \"Google Chrome\";v=\"118\", \"Not=A?Brand\";v=\"99\"")
req.Header.Add("sec-ch-ua-mobile", "?0")
req.Header.Add("sec-ch-ua-platform", "\"Windows\"")
req.Header.Add("sec-fetch-dest", "document")
req.Header.Add("sec-fetch-mode", "navigate")
req.Header.Add("sec-fetch-site", "same-origin")
req.Header.Add("sec-fetch-user", "?1")
req.Header.Add("upgrade-insecure-requests", "1")
req.Header.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")
req.Header.Add("Host", "hostloc.com")
req.Header.Add("Connection", "keep-alive")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
defer res.Body.Close()
if err != nil {
fmt.Println(err)
}
doc, err := htmlquery.Parse(res.Body)
if err != nil {
log.Fatal(err)
}
nodes := htmlquery.Find(doc, `//*[@id="threadlisttableid"]/tbody`)
fmt.Println(len(nodes))
for _, node := range nodes {
url := htmlquery.FindOne(node, "//tbody/tr/th/a[3]")
title := htmlquery.FindOne(node, "//tbody/tr/th/a[3]/text()")
reply := htmlquery.FindOne(node, "//tbody/tr/td[3]/a/text()")
view := htmlquery.FindOne(node, "//tbody/tr/td[3]/em/text()")
author := htmlquery.FindOne(node, "//tbody/tr/td[2]/cite/a/text()")
time := htmlquery.FindOne(node, "//tbody/tr/td[2]/em/span/span/text()")
if title == nil || time == nil {
continue
} else {
if htmlquery.InnerText(title) == "预览" {
continue
} else {
data = map[string]interface{}{
"标题": htmlquery.InnerText(title),
"链接": fmt.Sprint("https://hostloc.com/" + htmlquery.SelectAttr(url, "href")),
"回复": htmlquery.InnerText(reply),
"查看": htmlquery.InnerText(view),
"作者": htmlquery.InnerText(author),
"时间": htmlquery.InnerText(time),
}
datalist = append(datalist, data)
}
}
}
fmt.Println(len(datalist))
return datalist
}
func main() {
r := gin.Default()
v1 := r.Group("/v1")
{
go v1.GET("/hostloc", func(ctx *gin.Context) {
pages, _ := ctx.GetQuery("page")
byteva := getvalue(pages)
ctx.JSONP(http.StatusOK, byteva)
})
}
r.Run("127.0.0.1:8083")
}
© 版权声明
原创文章未经允许请勿转载。
THE END
请登录后查看评论内容