forked from dbtek/dbyll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawl_problem.go
74 lines (62 loc) · 1.55 KB
/
crawl_problem.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
package main
import (
"fmt"
"log"
"os"
"github.com/gocolly/colly/v2"
)
func main() {
//if len(os.Args) < 2 {
//fmt.Println("[ERROR] Missing problem number parameter.")
//return
//}
//problem := os.Args[1]
c := colly.NewCollector(
colly.MaxDepth(2),
colly.Async(),
)
c.Limit(&colly.LimitRule{DomainGlob: "*", Parallelism: 2})
//var result string
url := "https://leetcode.com/problems/two-sum/"
//https://leetcode.com/problems
//div role = "row"
// div role="cell"[1]
// 5번쨰 child a.href
// /problems/two-sum
//url += problem
c.OnHTML("div.main__2_tD" , func(e *colly.HTMLElement) {
fmt.Println(e)
fmt.Println(e.Text)
//result = "/**\n" + url + "\n\n" + result + "\n*/\n"
//result += "package main\n\n"
//result += "import (\n"
//result += " \"fmt\"\n"
//result += ")\n\n"
//result += "func main() {\n"
//result += " \n"
//result += "}"
})
// Start scraping
c.Visit(url)
// Wait until threads are finished
c.Wait()
// Write(Append) to file
//defer writeToFile(problem, result)
}
// problem: 문제번호
// desc: 문제설명
func writeToFile(problem, desc string) {
// If the
fname := "problems/" + problem + ".go"
f, err := os.OpenFile(fname, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
if _, err := f.Write([]byte(desc)); err != nil {
f.Close() // ignore error; Write error takes precedence
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
}