加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.go 1.50 KB
一键复制 编辑 原始数据 按行查看 历史
大龙猫 提交于 2021-12-08 21:23 . 添加命令输入
package main
import (
"bufio"
"fmt"
"golang.org/x/net/html"
"net/http"
"os"
"strings"
)
func main() {
/*words, images, _ := CountWordsAndImages(os.Args[1])
fmt.Printf("文字:%d,图片:%d \n", words, images)*/
readLine()
}
// readLine readLine
func readLine() {
r := bufio.NewReader(os.Stdin)
rawLine, _, _ := r.ReadLine()
line := string(rawLine)
fmt.Println(line)
}
// CountWordsAndImages does an HTTP GET request for the HTML
// document url and returns the number of words and images in it.
func CountWordsAndImages(url string) (words, images int, err error) {
resp, err := http.Get(url)
if err != nil {
return
}
doc, err := html.Parse(resp.Body)
resp.Body.Close()
if err != nil {
err = fmt.Errorf("parsing HTML: %s", err)
return
}
words, images = countWordsAndImages(doc)
//bare return
return
}
func countWordsAndImages(n *html.Node) (words, images int) {
texts, images := visit3(nil, 0, n)
for _, v := range texts {
v = strings.Trim(strings.TrimSpace(v), "\r\n")
if v == "" {
continue
}
words += strings.Count(v, "")
}
//bare return
return
}
//递归循环html
func visit3(texts []string, imgs int, n *html.Node) ([]string, int) {
//文本
if n.Type == html.TextNode {
texts = append(texts, n.Data)
}
//图片
if n.Type == html.ElementNode && (n.Data == "img") {
imgs++
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Data == "script" || c.Data == "style" {
continue
}
texts, imgs = visit3(texts, imgs, c)
}
//多返回值
return texts, imgs
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化