當前位置: 首頁>>代碼示例>>Golang>>正文


Golang NodeParser.Find方法代碼示例

本文整理匯總了Golang中github.com/zackshank/nookscraper/parser.NodeParser.Find方法的典型用法代碼示例。如果您正苦於以下問題:Golang NodeParser.Find方法的具體用法?Golang NodeParser.Find怎麽用?Golang NodeParser.Find使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/zackshank/nookscraper/parser.NodeParser的用法示例。


在下文中一共展示了NodeParser.Find方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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

}
開發者ID:zackshank,項目名稱:nookscraper,代碼行數:33,代碼來源:villager.go

示例2: parseAdditionalInformation

func (vp *VillagerParser) parseAdditionalInformation(url string, v *Villager, c chan bool) {
	np := parser.NodeParser{}
	// Get page
	resp, err := http.Get(url)
	if err != nil {
		c <- false
		return
	}

	defer resp.Body.Close()
	doc, err := html.Parse(resp.Body)
	if err != nil {
		c <- false
		return
	}

	// Find Table with Addt. Info
	_, mw := np.Find(doc, "id", "mw-content-text")
	_, table := np.Find(mw, "tag", "table")
	_, table = np.FindSibling(table, "tag", "table")

	// Attempt to retrieve birthday
	found, birthday := vp.parseBirthday(table)
	if found {
		v.Birthday = birthday
	}

	// Attemt to retrieve image
	found = vp.parseImage(table, fmt.Sprint(v.Name, ".png"))
	c <- true

}
開發者ID:zackshank,項目名稱:nookscraper,代碼行數:32,代碼來源:villager.go

示例3: FindVillagerListNode

func FindVillagerListNode(r *http.Response) *html.Node {
	np := parser.NodeParser{}
	doc, err := html.Parse(r.Body)
	if err != nil {
		fmt.Println("There was an error parsing the response: ", err.Error())
		return nil
	}
	_, mc := np.Find(doc, "id", "mw-content-text")
	_, vl := np.Find(mc, "tag", "table")
	return vl
}
開發者ID:zackshank,項目名稱:nookscraper,代碼行數:11,代碼來源:nookscraper.go

示例4: parsePersonality

func (vp *VillagerParser) parsePersonality(td *html.Node) (bool, string) {
	np := parser.NodeParser{}

	found, anode := np.Find(td, "tag", "a")
	if !found {
		return false, ""
	}

	textnode := anode.FirstChild
	if textnode.Type != html.TextNode {
		return false, ""
	}

	return true, string(textnode.Data)
}
開發者ID:zackshank,項目名稱:nookscraper,代碼行數:15,代碼來源:villager.go

示例5: parseJapaneseName

func (vp *VillagerParser) parseJapaneseName(td *html.Node) (bool, string) {
	np := parser.NodeParser{}

	found, namenode := np.Find(td, "tag", "b")
	if !found {
		return false, ""
	}

	textnode := namenode.FirstChild
	if textnode.Type != html.TextNode {
		return false, ""
	}

	return true, string(textnode.Data)
}
開發者ID:zackshank,項目名稱:nookscraper,代碼行數:15,代碼來源:villager.go

示例6: parseBirthday

func (vp *VillagerParser) parseBirthday(root *html.Node) (bool, *time.Time) {
	np := parser.NodeParser{}

	found, th := np.Find(root, "html", "Birthday")
	if !found {
		return false, nil
	}

	_, td := np.FindSibling(th, "tag", "td")
	_, anode := np.Find(td, "tag", "a")
	textelement := anode.FirstChild
	layout := "January 2"
	t, err := time.Parse(layout, string(textelement.Data))
	if err != nil {
		return false, nil
	}
	fmt.Printf("Found Birthday: %v\n", t)
	return true, &t
}
開發者ID:zackshank,項目名稱:nookscraper,代碼行數:19,代碼來源:villager.go

示例7: parseGames

func (vp *VillagerParser) parseGames(td *html.Node) (bool, []int) {
	np := parser.NodeParser{}

	var g []int

	for i := 1; i <= 8; i++ {
		found, _ := np.Find(td, "tag", "a")
		if found {
			g = append(g, i)
		}

		if i < 8 {
			var ok bool
			ok, td = np.FindSibling(td, "tag", "td")
			if !ok {
				return false, g
			}
		}
	}

	return true, g
}
開發者ID:zackshank,項目名稱:nookscraper,代碼行數:22,代碼來源:villager.go

示例8: 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
	}
//.........這裏部分代碼省略.........
開發者ID:zackshank,項目名稱:nookscraper,代碼行數:101,代碼來源:villager.go


注:本文中的github.com/zackshank/nookscraper/parser.NodeParser.Find方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。