我在做一个刮网机。给定一个特定的网页,我试图获取位于右上角的卖方的名称(在这个olx站点上,您可以看到卖方的名字是Ionut)。当我运行下面的代码时,它应该在index.csv文件中写入名称,但是文件是空的。我认为问题在HTML解析器上,尽管在我看来它很好。
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"path/filepath"
"github.com/gocolly/colly"
)
func main() {
//setting up the file where we store collected data
fName := filepath.Join("D:\\", "go projects", "cwst go", "CWST-GO", "target folder", "index.csv")
file, err := os.Create(fName)
if err != nil {
log.Fatalf("Could not create file, error :%q", err)
}
defer file.Close()
//writer that writes the collected data into our file
writer := csv.NewWriter(file)
//after the file is written, what it is in the buffer goes in writer and then passed to file
defer writer.Flush()
//collector
c := colly.NewCollector(
colly.AllowedDomains("https://www.olx.ro/"),
)
//HTML parser
c.OnHTML(".css-1fp4ipz", func(e *colly.HTMLElement) { //div class that contains wanted info
writer.Write([]string{
e.ChildText("h4"), //specific tag of the info
})
})
fmt.Printf("Scraping page : ")
c.Visit("https://www.olx.ro/d/oferta/bmw-xdrixe-seria-7-2020-71000-tva-IDgp7iN.html")
log.Printf("\n\nScraping Complete\n\n")
log.Println(c)
}发布于 2022-08-17 10:53:53
您不需要在允许的域中添加https或/。
c := colly.NewCollector(
colly.AllowedDomains("www.olx.ro"),
)https://stackoverflow.com/questions/73386406
复制相似问题