I need to scrape a website where the tag i'm interested into is:
<script type="myjson">
[{"class": "companyname", "location"....and so on
</script>
currently I am doing the job (goquery) with this code snippet:
doc.Find("script").Each(func(i int, element *goquery.Selection) {
_, exists := element.Attr("type")
if exists {
var filepath string
filepath = "mypath"
file, err := os.Create(filepath)
if err != nil {
panic("COULD NOT CREATE FILE")
}
file.WriteString(element.Text())
fmt.Println(element.Text())
file.Close()
the problem with this code is that while element.Text() is correctly printed to stdout (it prints a long slice with several jsons inside, which i need to print to a file for later work), the file.WriteString statement does not print anything to the file. The file remains empty.
It appears that my query is wrong and that it outputs 2 elements; the first with zero lenght, which is the one that is printed to the file, and the second with the real content, which is printed to stdout but not to the file.
Can you please suggest a correction to my code in order to print the content correctly to the file? I guess there may be an error in my goquery query.