I'm essentially a brand new Go programmer and to learn the ropes I figured I would make a simple web page scraper. Here is what it looks like:
package main
import (
"fmt"
"log"
"os"
"io"
)
func main() {
filename := "mh4u.txt"
file, err := os.Create(filename)
if err!= nil {
fmt.Println(err)
}
if err != nil {
log.Fatal(err)
}
doc.Find("tbody").Each(func(i int, s *goquery.Selection) {
// s here is a tbody element
s.Find("td").Each(func(j int, s2 *goquery.Selection) {
// s2 here is a td element
if s3 := s2.Find("img"); s3 != nil && s3.Length() > 0 {
return // This TD has at least one img child, skip it
}
fmt.Printf(s2.Text())
n, err := io.WriteString(file, s2.Text())
if err != nil {
fmt.Println(n, err) }
})
file.Close()
})
}
When trying to write out to the file I get the following error: 0 write mh4u.txt: The handle is invalid. I've been trying to look for any kind of reason why it wouldn't be able to write a string to the file but I just can't figure it out to save my life.