forked from sergiotapia/gophers
/
scrape.go
117 lines (103 loc) · 3.26 KB
/
scrape.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
package main
import (
"fmt"
"log"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
func scrapeSearch(document *goquery.Document, url string) {
pagesStr := document.Find("a.next_page").Prev().Text()
pages, _ := strconv.Atoi(pagesStr)
page := 1
for page <= pages {
pageURL := url + "&p=" + strconv.Itoa(page)
fmt.Println("Analyzing page: " + pageURL)
doc := downloadURL(pageURL)
doc.Find(".user-list-item").Each(func(i int, s *goquery.Selection) {
email := s.Find("a.email").Text()
profileURL, _ := s.Find("a").Eq(1).Attr("href")
username := profileURL[1:len(profileURL)]
profileURL = "http://github.com" + profileURL
info := s.Find(".user-list-info")
_ = info.Find("ul.user-list-meta").Remove()
_ = info.Find("a").Remove()
name := strings.TrimSpace(info.Text())
fmt.Println("Parsed user: " + username)
user := user{name: name, email: email, url: profileURL, username: username}
dumpToCSV(user)
})
page = page + 1
}
}
func scrapeProfile(document *goquery.Document) {
vcard := document.Find(".vcard")
email := vcard.Find("a.email").Text()
url := "http://github.com/" + vcard.Find(".vcard-username").Text()
username := vcard.Find(".vcard-username").Text()
name := vcard.Find(".vcard-fullname").Text()
fmt.Println("Parsed user: " + username)
user := user{name: name, email: email, url: url, username: username}
dumpToCSV(user)
}
func scrapeOrganization(document *goquery.Document, url string) {
nextPage := document.Find("a.next_page").Length()
if nextPage == 0 {
document.Find(".member-list-item").Each(func(i int, s *goquery.Selection) {
scrapeOrganizationItem(s)
})
} else {
pagesStr := document.Find("a.next_page").Prev().Text()
pages, _ := strconv.Atoi(pagesStr)
page := 1
for page <= pages {
pageURL := url + "?page=" + strconv.Itoa(page)
doc := downloadURL(pageURL)
doc.Find(".member-list-item").Each(func(i int, s *goquery.Selection) {
scrapeOrganizationItem(s)
})
page = page + 1
}
}
}
func scrapeOrganizationItem(element *goquery.Selection) {
url := "http://github.com/" + element.Find(".member-username").Text()
username := element.Find(".member-username").Text()
name := element.Find(".member-fullname").Text()
doc := downloadURL(url)
email := doc.Find(".vcard").Find("a.email").Text()
fmt.Println("Parsed user: " + username)
user := user{name: name, email: email, url: url, username: username}
dumpToCSV(user)
}
func scrapeStarGazers(document *goquery.Document, url string) {
totalStarGazers := 0
document.Find("ul.pagehead-actions li").Each(func(i int, s *goquery.Selection) {
if i == 1 {
count := strings.TrimSpace(s.Find("a.social-count").Text())
totalStarGazers, _ = strconv.Atoi(count)
}
})
pages := totalStarGazers / 30
page := 1
for page <= pages {
pageURL := url + "?page=" + strconv.Itoa(page)
doc := downloadURL(pageURL)
doc.Find(".follow-list-item span.css-truncate a").Each(func(i int, s *goquery.Selection) {
profileURL, _ := s.Attr("href")
profileURL = "http://github.com" + profileURL
profile := downloadURL(profileURL)
scrapeProfile(profile)
})
page = page + 1
}
}
func downloadURL(url string) *goquery.Document {
doc, err := goquery.NewDocument(url)
if err != nil {
log.Fatal(err)
}
time.Sleep(5 * time.Second)
return doc
}