-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (78 loc) · 2.55 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
package main
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/xuri/excelize/v2"
)
type ResponseData struct {
Text string `json:"text"`
URL string `json:"url"`
}
func scrapeHandler(w http.ResponseWriter, r *http.Request) {
url := r.URL.Query().Get("url")
if url == "" {
http.Error(w, "URL параметр отсутствует", http.StatusBadRequest)
return
}
response, err := http.Get(url)
if err != nil {
http.Error(w, fmt.Sprintf("Ошибка при HTTP-запросе: %v", err), http.StatusInternalServerError)
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
http.Error(w, fmt.Sprintf("Не удалось получить доступ к странице, статус код %d", response.StatusCode), http.StatusInternalServerError)
return
}
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Ошибка при чтении HTML-документа: %v", err), http.StatusInternalServerError)
return
}
var data []ResponseData
doc.Find("body").Each(func(i int, body *goquery.Selection) {
textData := strings.TrimSpace(body.Text())
lines := strings.Split(textData, "\n")
for _, line := range lines {
cleanLine := strings.TrimSpace(line)
if cleanLine != "" {
data = append(data, ResponseData{Text: cleanLine, URL: ""})
}
}
body.Find("a").Each(func(j int, a *goquery.Selection) {
href, exists := a.Attr("href")
if exists {
data = append(data, ResponseData{Text: "", URL: href})
}
})
})
f := excelize.NewFile()
index := f.NewSheet("Sheet1")
for i, item := range data {
if item.Text != "" {
cell := fmt.Sprintf("A%d", i+1)
f.SetCellValue("Sheet1", cell, item.Text)
} else if item.URL != "" {
cell := fmt.Sprintf("B%d", i+1)
f.SetCellValue("Sheet1", cell, item.URL)
}
}
f.SetActiveSheet(index)
w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
w.Header().Set("Content-Disposition", "attachment;filename=results.xlsx")
w.Header().Set("File-Name", "results.xlsx")
w.Header().Set("Content-Transfer-Encoding", "binary")
if err := f.Write(w); err != nil {
http.Error(w, fmt.Sprintf("Ошибка при записи Excel файла: %v", err), http.StatusInternalServerError)
}
}
func main() {
http.HandleFunc("/scrape", scrapeHandler)
fs := http.FileServer(http.Dir("./"))
http.Handle("/", fs)
log.Println("Сервер запущен на http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}