如何使用colly/goquery查找此html代码片段中的数值:
<body>
<a href="/xxxx/aaaa" > AAAA </a>, 125.00 <br>
<a href="/xxxx/bbbb" > BBBB </a>, 235.20 <br>
<a href="/xxxx/cccc" > CCCC </a>, 145.04 <br>
</body>发布于 2019-10-14 16:03:25
这段代码将把数字作为字符串部分,并包含空格。您需要修剪它们并将其解析为数字。
更新:代码现在可以裁剪行并解析为浮点型。
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"log"
"strconv"
"strings"
)
func main() {
html := `<body>
<a href="/xxxx/aaaa" > AAAA </a>, 125.00 <br>
<a href="/xxxx/bbbb" > BBBB </a>, 235.20 <br>
<a href="/xxxx/cccc" > CCCC </a>, 145.04 <br>
</body>`
reader := strings.NewReader(html)
doc, err := goquery.NewDocumentFromReader(reader)
if err != nil {
log.Fatal(err)
}
justText := doc.Text()
lines := strings.Split(justText, "\n")
for _, line := range lines {
if len(line) > 0 {
parts := strings.Split(line, ",")
number, err := strconv.ParseFloat(strings.TrimSpace(parts[1]), 64)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(number)
}
}
}
}https://stackoverflow.com/questions/58368892
复制相似问题