本文整理汇总了Golang中github.com/zackshank/nookscraper/parser.NodeParser.GetAttribute方法的典型用法代码示例。如果您正苦于以下问题:Golang NodeParser.GetAttribute方法的具体用法?Golang NodeParser.GetAttribute怎么用?Golang NodeParser.GetAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/zackshank/nookscraper/parser.NodeParser
的用法示例。
在下文中一共展示了NodeParser.GetAttribute方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: parseImage
func (vp *VillagerParser) parseImage(root *html.Node, filename string) bool {
np := parser.NodeParser{}
found, img := np.Find(root, "tag", "img")
if !found {
return false
}
ok, attr := np.GetAttribute(img, "src")
if !ok {
return false
}
resp, err := http.Get(fmt.Sprint(SiteRoot, attr.Val))
if err != nil {
return false
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false
}
err = ioutil.WriteFile(fmt.Sprint(ImgDir, "/villagers/", filename), body, 0644)
if err != nil {
return false
}
return true
}
示例2: Parse
func (vp *VillagerParser) Parse(tr *html.Node) (bool, *Villager) {
np := parser.NodeParser{}
ch := make(chan bool)
v := Villager{}
// Start Location
found, td := np.Find(tr, "tag", "th")
if !found {
fmt.Println("Could not parse Villager")
return false, nil
}
// Get name and start request to get the rest of the information
found, td = np.FindSibling(td, "tag", "th")
if !found {
fmt.Println("Could not find villager name")
return false, nil
}
// Get url to next location
ok, anode := np.Find(td, "tag", "a")
if ok {
_, attr := np.GetAttribute(anode, "href")
url := fmt.Sprint(SiteRoot, attr.Val)
go vp.parseAdditionalInformation(url, &v, ch)
}
found, v.Name = vp.parseName(td)
if !found {
fmt.Println("Could not find villager name")
return false, nil
}
// Get JapaneseName
found, td = np.FindSibling(td, "tag", "td")
if !found {
fmt.Println("Could not find villager japanese name")
return false, nil
}
found, v.JapaneseName = vp.parseJapaneseName(td)
if !found {
fmt.Println("Could not find villager japanese name")
return false, nil
}
// Get Species
found, td = np.FindSibling(td, "tag", "td")
if !found {
fmt.Println("Could not find villager species")
return false, nil
}
found, v.Species = vp.parseSpecies(td)
if !found {
fmt.Println("Could not find villager species")
return false, nil
}
// Get Gender
found, td = np.FindSibling(td, "tag", "td")
if !found {
fmt.Println("Could not find villager gender")
return false, nil
}
found, v.Gender = vp.parseGender(td)
if !found {
fmt.Println("Could not find villager gender")
return false, nil
}
// Get Personality
found, td = np.FindSibling(td, "tag", "td")
if !found {
fmt.Println("Could not find villager personality")
return false, nil
}
found, v.Personality = vp.parsePersonality(td)
if !found {
fmt.Println("Could not find villager personality")
return false, nil
}
// Get Games
found, td = np.FindSibling(td, "tag", "td")
if !found {
fmt.Println("Could not find villager games")
return false, nil
}
found, v.Games = vp.parseGames(td)
if !found {
fmt.Println("Could not find villager games")
return false, nil
}
//.........这里部分代码省略.........