我从Go开始,然后和Colly一起抓取。有人能帮我删除输出中的空行吗?这是我的代码:
package main
import (
"fmt"
"github.com/gocolly/colly"
)
func main() {
c := colly.NewCollector()
c.OnHTML("table > tbody", func(e *colly.HTMLElement) {
fmt.Println(e.Text)
})
c.Visit("https://www.olx.pl/elektronika/gdynia/")
}我尝试过这个:golang regexp remove all blank lines和这个:Golang idiomatic way to remove a blank line from a multi-line string,可能是以错误的方式,也可能是我遗漏了其他东西。
发布于 2020-02-28 02:15:33
嗯,看起来空行并不是真的空的,scraper只是不打印DOM中的元素,这就是为什么我看到抓取的文本之间有这么多间隙。这个问题是通过告诉scraper我想要抓取的子元素的特定定位器来解决的。
发布于 2020-04-06 13:50:32
我认为你可以尝试标准化空间。
func StandardizeSpaces(s string) string {
return strings.Join(strings.Fields(s), " ")
}
func main() {
c := colly.NewCollector()
c.OnHTML("table > tbody", func(e *colly.HTMLElement) {
fmt.Println(StandardizeSpaces(e.Text))
})
c.Visit("https://www.olx.pl/elektronika/gdynia/")
}https://stackoverflow.com/questions/60421517
复制相似问题