MCPcopy Index your code
hub / github.com/TruthHun/BookStack / BookStackCrawl

Method BookStackCrawl

models/document.go:611–700  ·  view source on GitHub ↗

爬虫批量采集 @param html html @param md markdown内容 @return content,markdown 把链接替换为标识后的内容

(html, md string, bookId, uid int)

Source from the content-addressed store, hash-verified

609//@param md markdown内容
610//@return content,markdown 把链接替换为标识后的内容
611func (m *Document) BookStackCrawl(html, md string, bookId, uid int) (content, markdown string, err error) {
612 var gq *goquery.Document
613 content = html
614 markdown = md
615 project := ""
616 if book, err := NewBook().Find(bookId); err == nil {
617 project = book.Identify
618 }
619 //执行采集
620 if gq, err = goquery.NewDocumentFromReader(strings.NewReader(content)); err == nil {
621 //采集模式mode
622 CrawlByChrome := false
623 if strings.ToLower(gq.Find("mode").Text()) == "chrome" {
624 CrawlByChrome = true
625 }
626 //内容选择器selector
627 selector := ""
628 if selector = strings.TrimSpace(gq.Find("selector").Text()); selector == "" {
629 err = errors.New("内容选择器不能为空")
630 return
631 }
632
633 // 截屏选择器
634 if screenshot := strings.TrimSpace(gq.Find("screenshot").Text()); screenshot != "" {
635 utils.ScreenShotProjects.Store(project, screenshot)
636 defer utils.DeleteScreenShot(project)
637 }
638
639 //排除的选择器
640 var exclude []string
641 if excludeStr := strings.TrimSpace(gq.Find("exclude").Text()); excludeStr != "" {
642 slice := strings.Split(excludeStr, ",")
643 for _, item := range slice {
644 exclude = append(exclude, strings.TrimSpace(item))
645 }
646 }
647
648 var links = make(map[string]string) //map[url]identify
649
650 gq.Find("a").Each(func(i int, selection *goquery.Selection) {
651 if href, ok := selection.Attr("href"); ok {
652 if !strings.HasPrefix(href, "$") {
653 hrefTrim := strings.TrimRight(href, "/")
654 identify := utils.MD5Sub16(hrefTrim) + ".md"
655 links[hrefTrim] = identify
656 links[href] = identify
657 }
658 }
659 })
660
661 gq.Find("a").Each(func(i int, selection *goquery.Selection) {
662 if href, ok := selection.Attr("href"); ok {
663 hrefLower := strings.ToLower(href)
664 //以http或者https开头
665 if strings.HasPrefix(hrefLower, "http://") || strings.HasPrefix(hrefLower, "https://") {
666 //采集文章内容成功,创建文档,填充内容,替换链接为标识
667 if retMD, err := utils.CrawlHtml2Markdown(href, 0, CrawlByChrome, 2, selector, exclude, links, map[string]string{"project": project}); err == nil {
668 var doc Document

Callers 1

ContentMethod · 0.80

Calls 8

FindMethod · 0.95
InsertOrUpdateMethod · 0.95
DeleteScreenShotFunction · 0.92
MD5Sub16Function · 0.92
CrawlHtml2MarkdownFunction · 0.92
NewBookFunction · 0.85
appendFunction · 0.50
ReplaceMethod · 0.45

Tested by

no test coverage detected