-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
52 lines (40 loc) · 1014 Bytes
/
Copy pathmain.go
File metadata and controls
52 lines (40 loc) · 1014 Bytes
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
package main
import (
"github.com/PuerkitoBio/goquery"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
)
func main() {
if len(os.Args) == 1 {
log.Fatal("parameter required, example: https://www.livelib.ru/reader/user_name/read/listview/biglist~")
}
var myUrl = os.Args[1]
const fileName = "out.txt"
fileContent := ""
for i := 1; ; i++ {
var currentPageUrl = myUrl + strconv.Itoa(i)
response, err := http.Get(currentPageUrl)
if err != nil {
log.Fatal(err)
}
document, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
log.Fatal("error loading HTTP response body. ", err)
}
if len(document.Find("span[itemprop='isbn']").Nodes) == 0 {
break
}
document.Find("span[itemprop='isbn']").Each(func(index int, element *goquery.Selection) {
html, _ := element.Html()
if html != "" {
fileContent += html + "\n"
}
})
response.Body.Close()
}
ioutil.WriteFile(fileName, []byte(fileContent), 0644)
log.Println("file successfully created")
}