go - 使用 Go-Colly 抓取时删除空行
问题描述
我从 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,可能是错误的方式,或者我错过了其他东西。
解决方案
我认为您可以尝试标准化空间。
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/")
}
推荐阅读
- java - 如何在 Gradle 构建文件中自动化 Doxygen 任务
- python - 请问如何获得一个类别中的列表数量,我试过了
- android - 以编程方式更改 android 片段中的选项菜单项 Kotlin
- angular - 转换可观察的
到 Angular 中的 T[] - android-studio - 我想知道如何通过 SwipeRefreshLayout 在 gridview 中随机显示图像
- python - 如何在不创建新列表的情况下比较列表中的值?
- python - 在 GeoPandas 中将纬度/经度点转换为网格多边形
- java - 使用 RxJavaErrorHandler 进行全局错误处理
- azure-devops - ADO 查询以显示父子以及孤儿 UserStories
- r - When I set the working directory in R, it doesn't recognize at first