-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
123 lines (95 loc) · 2.49 KB
/
main.go
File metadata and controls
123 lines (95 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/PuerkitoBio/goquery"
)
func checkFile(filename string) error {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
_, err := os.Create(filename)
if err != nil {
return err
}
}
return nil
}
type NaverQADB struct {
ID string `json:"id"`
Title string `json:"title"`
Question string `json:"question"`
Answers []string `json:"answer"`
Topic string `json:"topic"`
}
func main() {
filename := "webdata.json"
err := checkFile(filename)
if (err != nil){
log.Fatal(err)
}
file, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
url := "https://kin.naver.com/qna/detail.naver?d1id=6&dirId=60205&docId=450471426&qb=7J246rO17KeA64ql&enc=utf8§ion=kin&rank=1&search_sort=0&spq=0";
resp, err := http.Get(url);
if err != nil {
log.Fatal(err);
}
defer resp.Body.Close();
html, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Fatal(err)
}
data := []NaverQADB{}
json.Unmarshal(file, &data)
title := html.Find("div.c-heading__title-inner").Find("div.title").Text()
title = strings.TrimSpace(title)
topic := html.Find("a.tag-list__item.tag-list__item--category").Text()
topic = strings.Join(strings.Split(topic, " ")[2:], " ")
var temp_question []string
question := ""
_ = html.Find("div.c-heading__content").Find("div").Each(func(idx int, sel *goquery.Selection){
t := sel.Text()
t = strings.TrimSpace(t)
temp_question = append(temp_question, t)
})
question = strings.Join(temp_question, "\n")
fmt.Println(question)
var answers []string
_ = html.Find("div._answerListArea").Find("div.answer-content__list._answerList").Each(func(idx int, s *goquery.Selection){
var temp_answer []string
answer := ""
s.Find(".se-module.se-module-text").Find("p").Each(func(idx int, sel *goquery.Selection) {
t := sel.Text()
t = strings.TrimSpace(t)
temp_answer = append(temp_answer, t)
fmt.Println(t)
})
answer = strings.Join(temp_answer, "\n")
if (answer != ""){
answers = append(answers, answer)
}
})
newRow := &NaverQADB{
ID: "1234",
Title: title,
Question: question,
Answers: answers,
Topic: topic,
}
data = append(data, *newRow)
dataBytes, err := json.Marshal(data)
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(filename, dataBytes, 0644)
if err != nil {
log.Fatal(err)
}
}