simple web scraper won't write to a .txt file

99 views
Skip to first unread message

fullmeta...@gmail.com

unread,
Jun 2, 2015, 8:34:22 AM6/2/15
to golan...@googlegroups.com
Hey guys,

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)
}
        doc, err := goquery.NewDocument("http://monsterhunter.wikia.com/wiki/MH4:_Item_List")
        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.

Thanks so much for any tips/suggestions/solutions all are greatly appreciated!

Jan Mercl

unread,
Jun 2, 2015, 9:07:10 AM6/2/15
to golan...@googlegroups.com
On Tue, Jun 2, 2015 at 2:34 PM <fullmeta...@gmail.com> wrote:

The file is created once. It is also closed after each write to it, so since the second attempt it'll fail.

-j
--

-j

Tallal Garghouti

unread,
Jun 2, 2015, 10:07:22 AM6/2/15
to golan...@googlegroups.com, fullmeta...@gmail.com

Looks like a good case for defer to me.


Regards, T



Lars Tørnes Hansen

unread,
Jun 2, 2015, 11:14:06 AM6/2/15
to golan...@googlegroups.com, fullmeta...@gmail.com
It is not a good idea to just fmt.Println/fmt.Printf/log.Printf/log.Println the error, and then continue as no error had happened.

/Lars


Den tirsdag den 2. juni 2015 kl. 14.34.22 UTC+2 skrev fullmeta...@gmail.com:
Hey guys,

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

....

if err!= nil {
fmt.Println(err)
}
... 
Reply all
Reply to author
Forward
0 new messages