Created
August 16, 2016 18:07
-
-
Save tkc/a60e163df61d600ac20760fe036422b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
go get github.com/PuerkitoBio/goquery" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/PuerkitoBio/goquery" | |
"fmt" | |
) | |
type Element struct { | |
Url string | |
Title string | |
H1 string | |
H2 string | |
P string | |
A string | |
Img string | |
} | |
func getElements(url string) Element { | |
doc, err := goquery.NewDocument(url) | |
if err != nil { | |
fmt.Print("err") | |
} | |
titles := doc.Find("title") | |
title := titles.Eq(0) | |
h1s := doc.Find("h1") | |
h1 := h1s.Eq(0) | |
h2s := doc.Find("h2") | |
h2 := h2s.Eq(0) | |
ps := doc.Find("p") | |
p := ps.Eq(0) | |
as := doc.Find("a") | |
a := as.Eq(0) | |
fmt.Println(p.Text()) | |
aAttr, _ := a.Attr("href") | |
imgs := doc.Find("img") | |
img := imgs.Eq(0) | |
fmt.Println(img.Text()) | |
imgAttr, _ := img.Attr("src") | |
return Element{ | |
Url:url, | |
Title:title.Text(), | |
H1:h1.Text(), | |
H2:h2.Text(), | |
P:p.Text(), | |
A:aAttr, | |
Img:imgAttr, | |
} | |
} | |
func main() { | |
url := "http://onga-tec.hatenadiary.jp/" | |
element := getElements(url) | |
fmt.Println(element) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment