代码拉取完成,页面将自动刷新
package fer
import (
"context"
"time"
"gitee.com/k3x/urlx"
"gitee.com/k3x/urlx/codec/html"
)
func FromBOC(ctx context.Context) (result Rates, err error) {
defer result.SetDuration(time.Now())
result.From = "中国银行"
var rates []struct {
Name string `find:"td:nth-child(1)"` // 货币名称
BuyingRate float64 `find:"td:nth-child(2)"` // 汇买价
CashBuyingRate float64 `find:"td:nth-child(3)"` // 钞买价
SellingRate float64 `find:"td:nth-child(4)"` // 汇卖价
CashSellingRate float64 `find:"td:nth-child(5)"` // 钞卖价
MiddleRate float64 `find:"td:nth-child(6)"` // 中间价
PubTime string `find:"td:nth-child(7)"`
}
err = urlx.MacEdge(ctx).Url("https://www.boc.cn/sourcedb/whpj/index_1.html").
HeaderWith(urlx.Referer("https://www.boc.cn")).
Process(html.Struct(&rates, "table tr:has(td.pjrq)"))
result.Data = make(map[string]Rate, len(rates))
if len(rates) > 0 {
for i, r := range rates {
if i == 0 {
result.UpdateAt, _ = time.ParseInLocation("2006.01.02 15:04:05", r.PubTime, locCST)
}
currency := GetCode(r.Name)
result.Data[currency] = Rate{
Name: r.Name,
Currency: currency,
BuyingRate: r.BuyingRate,
CashBuyingRate: r.CashBuyingRate,
SellingRate: r.SellingRate,
CashSellingRate: r.CashSellingRate,
MiddleRate: r.MiddleRate,
}
}
}
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。