6

[Golang] Get HTML Title via goquery

 3 years ago
source link: http://siongui.github.io/2016/03/22/go-get-html-title-via-goquery/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

[Golang] Get HTML Title via goquery

March 22, 2016

A simple example to read HTML title via goquery in Golang.

Install goquery:

$ go get -u github.com/PuerkitoBio/goquery

Source code:

title.go | repository | view raw

package main

import (
	"flag"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"os"
)

func processHTML(path string) {
	f, err := os.Open(path)
	if err != nil {
		panic(err)
	}
	defer f.Close()

	doc, err := goquery.NewDocumentFromReader(f)
	if err != nil {
		panic(err)
	}

	title := doc.Find("title").Text()
	fmt.Println(title)
}

func main() {
	pPath := flag.String("input", "", "Path of file to be processed")
	flag.Parse()
	path := *pPath
	if path == "" {
		fmt.Fprintf(os.Stderr, "Error: empty path!\n")
		return
	}

	processHTML(path)
}

Command line usage:

$ go run title.go -input=index.html

Tested on: Ubuntu Linux 15.10, Go 1.6.


References:

[3]read html title · twnanda/twnanda@5d81787 · GitHub


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK