-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
129 lines (101 loc) · 2.33 KB
/
main.go
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
124
125
126
127
128
129
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"unicode"
)
var wg sync.WaitGroup
func createFolder() string {
currentTime := time.Now()
timeFormatted := currentTime.Format("2006_01_02")
folderName := fmt.Sprintf("%s-%s", timeFormatted, "webpage")
err := os.Mkdir(folderName, 0755)
if err != nil {
fmt.Println("folder already exist", err)
return folderName
}
return folderName
}
func downloadHtml(url string, folderName string, title string, num string) {
defer wg.Done()
response, err := http.Get(url)
if err != nil {
fmt.Println("Error while fetching the web page:", err)
return
}
defer response.Body.Close()
htmlBytes, err := io.ReadAll(response.Body)
if err != nil {
fmt.Println("Error while reading response body:", err)
return
}
filePath := fmt.Sprintf("%s/%s-%s.html", folderName, title, num) // 폴더 내에 파일 경로 설정
err = os.WriteFile(filePath, htmlBytes, 0644)
if err != nil {
fmt.Println("Error while writing to file:", err)
return
}
fmt.Println("Web page HTML saved to", filePath)
}
func getUrls(rootUrl string) map[string]string {
response, err := http.Get(rootUrl)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
log.Fatal(err)
}
linkTextMap := make(map[string]string)
doc.Find("a").Each(func(i int, s *goquery.Selection) {
link, exists := s.Attr("href")
if !exists {
return
}
text := strings.TrimSpace(s.Text())
if strings.Contains(link, "article") {
linkTextMap[link] = text
}
})
return linkTextMap
}
func makeTitle(text string) string {
substring := text
if len(text) > 20 {
substring = text[:20]
}
var cleanedSubstring strings.Builder
for _, char := range substring {
if unicode.IsLetter(char) {
cleanedSubstring.WriteRune(char)
} else if unicode.IsSpace(char) {
cleanedSubstring.WriteRune('_')
}
}
result := cleanedSubstring.String()
return result
}
func main() {
folderName := createFolder()
if folderName == "" {
return
}
urls := getUrls("https://news.naver.com")
num := 0
for link, text := range urls {
num++
wg.Add(1)
go downloadHtml(link, folderName, makeTitle(text), strconv.Itoa(num))
}
wg.Wait()
fmt.Printf("%d개 저장 완료\n", num)
}