htct

package module
v0.0.0-...-7dbae93 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 9 Imported by: 0

README

htct

从html文本中提取标题,正文,图片,作者,时间等信息,适用于新闻类网页

安装

    go get github.com/23233/ggg/htct

使用

package main

import (
    "io/ioutil"
	"log"
	"net/http"

	"github.com/23233/ggg/htct"
)

func main(){
    url := "http://www.xxx.com/xxx"
	resp, err := http.Get(url)
	if err != nil {
		log.Fatal(err)
	}
	source, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
    }
    // 这只是一个例子
    // htct.Extract 接收一个html的字符串
    // 可根据需求自行选择如何获取一个html字符串
    // 例如带分页的页面,可自行获取所有分页内容,拼接后传入
    result, _ := htct.Extract(string(source),url)
    fmt.Printf("%+v", result)
}

说明

htct使用的《基于文本及符号密度的网页正文提取方法》对于一般的中文新闻类网页有较高的准确率,根据论文结论可知准确率高达99%以上.但由于样本条件限制作者并未测试足够多的样本来验证准确率.
由于网页代码的多样性,任何提取算法都不可能覆盖所有网页.如遇到不能正确提取的网页,欢迎在issue中留下网页地址,具体问题具体分析.作者尽可能的去完善,以覆盖更多的页面.

htct 命令行是为了方便测试和调试使用, 只是简单的 wget + extract , 并不能解析由js生成的动态页面, 动态页面可自行选择使用合适的解析办法.

感谢

本项目受到 github.com/kingname/GeneralNewsExtractor 的启发,并参考使用了它的测试用例用进行开发和测试

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LookupIPAddresses

func LookupIPAddresses(domain string) (string, string, error)

LookupIPAddresses 根据给定的域名返回一个IPv4和一个IPv6地址(如果存在的话) domain必须不包含协议头

Types

type Article

type Article struct {
	MetaTitle       string `json:"meta_title,omitempty" bson:"meta_title,omitempty"`
	MetaDescription string `json:"meta_description,omitempty" bson:"meta_description,omitempty"`
	MetaKeywords    string `json:"meta_keywords,omitempty" bson:"meta_keywords,omitempty"`
	Url             string `json:"url,omitempty" bson:"url,omitempty"` // 当前的url
	Icp             string `json:"icp,omitempty" bson:"icp,omitempty"` // icp备案号
	Ipv4            string `json:"ipv_4,omitempty" bson:"ipv_4,omitempty"`
	Ipv6            string `json:"ipv_6,omitempty" bson:"ipv_6,omitempty"`
	// ContentTitle 标题
	ContentTitle string `json:"content_title,omitempty" bson:"content_title,omitempty"`
	// ContentImages 图片
	ContentImages []string `json:"content_images,omitempty" bson:"content_images,omitempty"`
	// Author 作者
	Author string `json:"author,omitempty" bson:"author,omitempty"`
	// PublishTime 发布时间
	PublishTime string `json:"publish_time,omitempty" bson:"publish_time,omitempty"`
	// Content 正文
	Content string `json:"content,omitempty" bson:"content,omitempty"`
	// ContentLine 内容段落 以`\n`为切分标准
	ContentLine []string `json:"content_line,omitempty" bson:"content_line,omitempty"`
	// ContentHTML 正文源码
	ContentHTML string `json:"content_html,omitempty" bson:"content_html,omitempty"`
	// AllLinks 所有链接 内外链
	AllLinks []KvMap `json:"all_links,omitempty" bson:"all_links,omitempty"`
}

Article 提取的文本信息

func Extract

func Extract(source string, sourceUrl string) (*Article, error)

Extract 提取信息

type KvMap

type KvMap struct {
	Key string `json:"key"`
	Val string `json:"val"`
}

Jump to

Keyboard shortcuts

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