dom

package
v0.0.0-...-2226738 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Example

Example demonstrates a technique for getting the top 3 language views per day in Wikipedia.

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/Hunsin/go-htmlutil/dom"
)

func main() {
	req, err := http.NewRequest(http.MethodGet, "https://www.wikipedia.org", nil)
	if err != nil {
		log.Fatalln(err)
	}

	doc, err := dom.Fetch(req)
	if err != nil {
		log.Fatalln(err)
	}

	top3 := doc.GetElementsByClassName("central-featured-lang")[:3]
	for _, elm := range top3 {
		lang := elm.GetElementsByTagName("strong")[0].TextContent()
		link := elm.FirstElementChild().GetAttribute("href")
		fmt.Println(lang, link)
	}
}
Output:

English //en.wikipedia.org/
日本語 //ja.wikipedia.org/
Español //es.wikipedia.org/

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Document

type Document interface {
	Node

	Body() Element
	Children() []Element
	GetElementByID(id string) Element
	GetElementsByClassName(string) []Element
	GetElementsByName(string) []Element
	GetElementsByTagName(string) []Element
	Head() Element
	Links() []Element
	Title() string
}

A Document represents the DOM Document interface, which is the entry point a web page's content.

func Fetch

func Fetch(req *http.Request) (Document, error)

Fetch sends the HTTP request and returns the DOM tree parsed from the response.

func Parse

func Parse(r io.Reader) (Document, error)

Parse returns the DOM tree parsed from the given Reader.

type Element

type Element interface {
	Node

	Children() []Element
	ClassList() []string
	ClassName() string
	FirstElementChild() Element
	GetAttribute(string) string
	GetElementsByClassName(string) []Element
	GetElementsByTagName(string) []Element
	ID() string
	LastElementChild() Element
	NextElementSibling() Element
	ParentElement() Element
	PreviousElementSibling() Element
	TagName() string
}

An Element represents the DOM Element interface.

type Node

type Node interface {
	TextContent() string
}

A Node represents the DOM Node interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL