我是靠谱客的博主 谦让酸奶,这篇文章主要介绍《Go 指南》 练习:Web 爬虫 || A Tour of Go , Exercise: Web Crawler,现在分享给大家,希望可以做个参考。

练习地址: Go指南

关键代码段:

注意 wg.Done 的位置不能放到 Crawl 方法中 sc.wg.Add(1) 后面,否则会出问题。【参考链接中的规则一

Unlock同理

 

复制代码
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
type SafeCounter struct { mp map[string]bool mux sync.Mutex wg sync.WaitGroup } // Crawl 使用 fetcher 从某个 URL 开始递归的爬取页面,直到达到最大深度。 func (sc *SafeCounter)Crawl(url string, depth int, fetcher Fetcher) { defer sc.wg.Done() sc.mux.Lock() sc.mp[url] = true defer sc.mux.Unlock() if depth <= 0 { return } body, urls, err := fetcher.Fetch(url) if err != nil { fmt.Println(err) return } fmt.Printf("found: %s %qn", url, body) for _, u := range urls { if sc.mp[u] == false{ sc.wg.Add(1) go sc.Crawl(u, depth-1, fetcher) } } return } func main() { c := SafeCounter{mp: make(map[string]bool)} c.wg.Add(1) c.Crawl( "https://golang.org/", 4, fetcher) c.wg.Wait() }

 

 

 

 

 

 

 

all code:

复制代码
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
package main import ( "fmt" "sync" ) type Fetcher interface { // Fetch 返回 URL 的 body 内容,并且将在这个页面上找到的 URL 放到一个 slice 中。 Fetch(url string) (body string, urls []string, err error) } type SafeCounter struct { mp map[string]bool mux sync.Mutex wg sync.WaitGroup } // Crawl 使用 fetcher 从某个 URL 开始递归的爬取页面,直到达到最大深度。 func (sc *SafeCounter)Crawl(url string, depth int, fetcher Fetcher) { defer sc.wg.Done() sc.mux.Lock() sc.mp[url] = true defer sc.mux.Unlock() if depth <= 0 { return } body, urls, err := fetcher.Fetch(url) if err != nil { fmt.Println(err) return } fmt.Printf("found: %s %qn", url, body) for _, u := range urls { if sc.mp[u] == false{ sc.wg.Add(1) go sc.Crawl(u, depth-1, fetcher) } } return } func main() { c := SafeCounter{mp: make(map[string]bool)} c.wg.Add(1) c.Crawl( "https://golang.org/", 4, fetcher) c.wg.Wait() } // fakeFetcher 是返回若干结果的 Fetcher。 type fakeFetcher map[string]*fakeResult type fakeResult struct { body string urls []string } func (f fakeFetcher) Fetch(url string) (string, []string, error) { if res, ok := f[url]; ok { return res.body, res.urls, nil } return "", nil, fmt.Errorf("not found: %s", url) } // fetcher 是填充后的 fakeFetcher。 var fetcher = fakeFetcher{ "https://golang.org/": &fakeResult{ "The Go Programming Language", []string{ "https://golang.org/pkg/", "https://golang.org/cmd/", }, }, "https://golang.org/pkg/": &fakeResult{ "Packages", []string{ "https://golang.org/", "https://golang.org/cmd/", "https://golang.org/pkg/fmt/", "https://golang.org/pkg/os/", }, }, "https://golang.org/pkg/fmt/": &fakeResult{ "Package fmt", []string{ "https://golang.org/", "https://golang.org/pkg/", }, }, "https://golang.org/pkg/os/": &fakeResult{ "Package os", []string{ "https://golang.org/", "https://golang.org/pkg/", }, }, }

 

最后

以上就是谦让酸奶最近收集整理的关于《Go 指南》 练习:Web 爬虫 || A Tour of Go , Exercise: Web Crawler的全部内容,更多相关《Go内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(117)

评论列表共有 0 条评论

立即
投稿
返回
顶部