Go library that cleans a HTML page for better readability.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
huiyifyj 5d85eb9d4b
Update README and upgrade go version information
12 months ago
cmd/go-readability Init repo 1 year ago
scripts Init repo 1 year ago
test-pages Init repo 1 year ago
.gitattributes Init repo 1 year ago
.gitignore Init repo 1 year ago
LICENSE Init repo 1 year ago
README.md Update README and upgrade go version information 12 months ago
go.mod Update README and upgrade go version information 12 months ago
parser.go Init repo 1 year ago
parser_test.go Init repo 1 year ago
readability.go Init repo 1 year ago
utils-common.go Init repo 1 year ago
utils-common_test.go Init repo 1 year ago
utils-node.go Init repo 1 year ago
utils-node_test.go Init repo 1 year ago

README.md

Readability

GoDoc LICENSE

📖 Readability 是 go 语言的库, 基于 DOM 结构特征, 从 HTML 页面中找到主要的可读文字内容和一些元数据(如作者、标题、摘要等)。并消去和移除大部分网页中混乱的按钮, 广告, 背景图片, 以及一些 JS 脚本...

安装方法

安装该库只需运行命令行 go get:

go get -u -v gitea.com/huiyifyj/readability

使用案例

使用 readability.FromURL() 方法就可以通过 URL 参数去获取文字内容。它将会从指定的 URL 中获取网页, 检查其是否可读, 然后返回已经找到的文字内容:

package main

import (
	"fmt"
	"log"
	"os"
	"time"

	readability "gitea.com/huiyifyj/readability"
)

var (
	urls = []string{
		// 这是一篇可解析的文字
		"https://huiyifyj.cn/a-case-for-using-void-in-modern-javascript/",
		// 这并不是一篇文字, 所以它可能会解析失败
		"https://huiyifyj.cn",
	}
)

func main() {
	for i, url := range urls {
		article, err := readability.FromURL(url, 30*time.Second)
		if err != nil {
			log.Fatalf("Failed to parse %s, %v\n", url, err)
		}

		dstTxtFile, _ := os.Create(fmt.Sprintf("text-%02d.txt", i+1))
		defer dstTxtFile.Close()
		dstTxtFile.WriteString(article.TextContent)

		dstHTMLFile, _ := os.Create(fmt.Sprintf("html-%02d.html", i+1))
		defer dstHTMLFile.Close()
		dstHTMLFile.WriteString(article.Content)

		fmt.Printf("URL     : %s\n", url)
		fmt.Printf("Title   : %s\n", article.Title)    // 文章标题
		fmt.Printf("Author  : %s\n", article.Byline)   // 文章作者
		fmt.Printf("Length  : %d\n", article.Length)   // 文章长度
		fmt.Printf("Excerpt : %s\n", article.Excerpt)  // 文章摘要
		fmt.Printf("SiteName: %s\n", article.SiteName) // 站点名称
		fmt.Printf("Image   : %s\n", article.Image)    // 文章图片
		fmt.Printf("Favicon : %s\n", article.Favicon)  // 文章图标
		fmt.Printf("Text content saved to \"text-%02d.txt\"\n", i+1)
		fmt.Printf("HTML content saved to \"html-%02d.html\"\n", i+1)
		fmt.Println()
	}
}

但是, 有时候无论该 URL 的内容是不是文章, 你都想去解析。比如, 你只是想去获取该页面的元数据(如, 网页的标题、摘要等等)。 那么, 你就需要使用 http.Get 将 URL 的页面下载下来, 然后使用 readability.FromReader() 对其进行解析:

package main

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

	readability "gitea.com/huiyifyj/readability"
)

var (
	urls = []string{
		// 文字内容和元数据都能正常解析出来
		"https://huiyifyj.cn/a-case-for-using-void-in-modern-javascript",
		// 但是这个不会解析出文字内容, 但是可以解析出元数据
		"https://huiyifyj.cn",
	}
)

func main() {
	for _, url := range urls {
		resp, err := http.Get(url)
		if err != nil {
			log.Fatalf("Failed to download %s: %v\n", url, err)
		}
		defer resp.Body.Close()

		article, err := readability.FromReader(resp.Body, url)
		if err != nil {
			log.Fatalf("Failed to parse %s: %v\n", url, err)
		}

		fmt.Printf("URL     : %s\n", url)
		fmt.Printf("Title   : %s\n", article.Title)
		fmt.Printf("Author  : %s\n", article.Byline)
		fmt.Printf("Length  : %d\n", article.Length)
		fmt.Printf("Excerpt : %s\n", article.Excerpt)
		fmt.Printf("SiteName: %s\n", article.SiteName)
		fmt.Printf("Image   : %s\n", article.Image)
		fmt.Printf("Favicon : %s\n", article.Favicon)
		fmt.Println()
	}
}

命令行使用

你也可以使用 go-readability 作为命令行使用。只需要通过以下命令安装:

go get -u -v gitea.com/huiyifyj/readability/cmd/...

安装后你就可以在终端界面使用 go-readability:

$ go-readability -h

go-readability is parser to fetch the readable content of a web page.
The source can be an url or existing file in your storage.

Usage:
  go-readability [flags] source

Flags:
  -h, --help       help for go-readability
  -m, --metadata   only print the page's metadata

协议

Readability 库遵守 MIT 协议

这意味你可以随意改动, 只要注明我 @huiyifyj 即可。