Skip to content

Commit

Permalink
Merge pull request gocolly#172 from vosmith/localfile_example
Browse files Browse the repository at this point in the history
Added local files example
  • Loading branch information
asciimoo authored Jun 25, 2018
2 parents 41323a4 + 1d5f06a commit e7ff7f4
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 1 deletion.
12 changes: 12 additions & 0 deletions _examples/local_files/html/child_page/one.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Child Page One</h1>
</body>
</html>
12 changes: 12 additions & 0 deletions _examples/local_files/html/child_page/three.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Child Page Three</h1>
</body>
</html>
12 changes: 12 additions & 0 deletions _examples/local_files/html/child_page/two.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Child Page Two</h1>
</body>
</html>
17 changes: 17 additions & 0 deletions _examples/local_files/html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Index.html</h1>
<ul>
<li><a href="/child_page/one.html"></a></li>
<li><a href="/child_page/two.html"></a></li>
<li><a href="/child_page/three.html"></a></li>
</ul>
</body>
</html>
Binary file added _examples/local_files/local_files
Binary file not shown.
40 changes: 40 additions & 0 deletions _examples/local_files/local_files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"fmt"
"net/http"
"os"
"path/filepath"

"github.com/gocolly/colly"
)

func main() {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err)
}

t := &http.Transport{}
t.RegisterProtocol("file", http.NewFileTransport(http.Dir("/")))

c := colly.NewCollector()
c.WithTransport(t)

pages := []string{}

c.OnHTML("h1", func(e *colly.HTMLElement) {
pages = append(pages, e.Text)
})

c.OnHTML("a", func(e *colly.HTMLElement) {
c.Visit("file://" + dir + "/html" + e.Attr("href"))
})

fmt.Println("file://" + dir + "/html/index.html")
c.Visit("file://" + dir + "/html/index.html")
c.Wait()
for i, p := range pages {
fmt.Printf("%d : %s\n", i, p)
}
}
4 changes: 3 additions & 1 deletion http_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ func (h *httpBackend) Do(request *http.Request, bodySize int) (*Response, error)
if err != nil {
return nil, err
}
*request = *res.Request
if res.Request != nil {
*request = *res.Request
}

var bodyReader io.Reader = res.Body
if bodySize > 0 {
Expand Down

0 comments on commit e7ff7f4

Please sign in to comment.