2 Star 1 Fork 0

k3x/fer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
spider_bank_of_china.go 2.03 KB
一键复制 编辑 原始数据 按行查看 历史
k3x 提交于 2022-01-16 09:18 . init
package fer
import (
"context"
"fmt"
"strings"
"time"
"gitee.com/k3x/urlx"
"gitee.com/k3x/urlx/codec/html"
)
type Options struct {
Currency string
Date time.Time
}
func FromBankOfChina(ctx context.Context, options Options) (result Rates, err error) {
defer result.SetDuration(time.Now())
result.From = "中国银行"
if options.Currency == "" {
options.Currency = "0"
}
if options.Date.IsZero() {
options.Date = time.Now()
}
erectDate := options.Date.Format("2006-01-02")
searchUri := "https://srh.bankofchina.com/search/whpj/searchen.jsp"
var out []struct {
Currency string `find:"td:nth-child(1)" json:"currency"` // 货币
BuyingRate float64 `find:"td:nth-child(2)" json:"buying_rate"`
CashBuyingRate float64 `find:"td:nth-child(3)" json:"cash_buying_rate"`
SellingRate float64 `find:"td:nth-child(4)" json:"selling_rate"`
CashSellingRate float64 `find:"td:nth-child(5)" json:"cash_selling_rate"`
MiddleRate float64 `find:"td:nth-child(6)" json:"middle_rate"`
PubTime string `find:"td:nth-child(7)" json:"pub_time"`
}
for page := 1; page <= 2; page++ {
err = urlx.MacEdge(ctx).
Url(searchUri).
Method(urlx.MethodPost).
HeaderWith(urlx.Referer(searchUri)).
Form(strings.NewReader(fmt.Sprintf(`erectDate=%s&nothing=%s&pjname=%s&page=%d`, erectDate, erectDate, options.Currency, page))).
Process(html.Struct(&out, "table[bgcolor$=EAEAEA] tr+tr"))
if err != nil || len(out) != 20 {
break
}
}
result.Data = make(map[string]Rate, len(out))
if len(out) > 0 {
for i, r := range out {
if i == 0 {
result.UpdateAt, _ = time.ParseInLocation("2006.01.02 15:04:05", r.PubTime, locCST)
}
if _, find := result.Data[r.Currency]; find {
break
}
result.Data[r.Currency] = Rate{
Name: GetLabel(r.Currency),
Currency: r.Currency,
BuyingRate: r.BuyingRate,
CashBuyingRate: r.CashBuyingRate,
SellingRate: r.SellingRate,
CashSellingRate: r.CashSellingRate,
MiddleRate: r.MiddleRate,
}
}
}
return
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/k3x/fer.git
[email protected]:k3x/fer.git
k3x
fer
fer
main

搜索帮助