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

Function CrawlHtml2Markdown

utils/util.go:386–562  ·  view source on GitHub ↗

采集HTML并把相对链接和相对图片 内容类型,contType:0表示markdown,1表示html,2表示文本 force:是否是强力采集 intelligence:是否是智能提取,智能提取,使用html2article,否则提取body diySelecter:自定义选择器 注意:由于参数问题,采集并下载图片的话,在headers中加上key为"project"的字段,值为书籍的标识

(urlstr string, contType int, force bool, intelligence int, diySelector string, excludeSelector []string, links map[string]string, headers ...map[string]string)

Source from the content-addressed store, hash-verified

384// diySelecter:自定义选择器
385// 注意:由于参数问题,采集并下载图片的话,在headers中加上key为"project"的字段,值为书籍的标识
386func CrawlHtml2Markdown(urlstr string, contType int, force bool, intelligence int, diySelector string, excludeSelector []string, links map[string]string, headers ...map[string]string) (cont string, err error) {
387 defer func() {
388 if r := recover(); r != nil {
389 err = errors.New(fmt.Sprint(r))
390 return
391 }
392 }()
393
394 //记录已经存在了的图片,避免重复图片出现重复采集的情况
395 var existImage bool
396
397 from := "\r\n<!-- 原文:" + urlstr + " -->"
398
399 imageMap := make(map[string]string)
400
401 if strings.Contains(urlstr, "bookstack.cn") {
402 return
403 }
404
405 // 默认记录到数据库中的图片路径
406 //save := src
407 project := ""
408 for _, header := range headers {
409 if val, ok := header["project"]; ok {
410 project = val
411 }
412 }
413
414 if force { //强力模式
415 cont, err = CrawlByChrome(urlstr, project)
416 } else {
417 req := util.BuildRequest("get", urlstr, "", "", "", true, false, headers...).SetTimeout(httpTimeout, httpTimeout)
418 cont, err = req.String()
419 if err != nil && transfer != "" {
420 req := util.BuildRequest("get", transfer+urlstr, urlstr, "", "", true, false, headers...).SetTimeout(httpTimeout, httpTimeout)
421 cont, err = req.String()
422 }
423 }
424
425 cont = strings.Replace(cont, "¶", "", -1)
426
427 if err != nil {
428 return
429 }
430
431 var doc *goquery.Document
432 doc, err = goquery.NewDocumentFromReader(strings.NewReader(cont))
433 if err != nil {
434 return
435 }
436
437 //遍历a标签替换相对链接
438 doc.Find("a").Each(func(i int, selection *goquery.Selection) {
439 //存在href,且不以http://和https://开头
440 if href, ok := selection.Attr("href"); ok {
441 href = JoinURL(urlstr, href)
442
443 if link, ok := links[strings.TrimRight(href, "/")]; ok {

Callers 2

CrawlMethod · 0.92
BookStackCrawlMethod · 0.92

Calls 11

FindMethod · 0.95
ConvertFunction · 0.92
CrawlByChromeFunction · 0.85
JoinURLFunction · 0.85
DownImageFunction · 0.85
HandleSVGFunction · 0.85
MoveToStoreMethod · 0.80
MoveToOssMethod · 0.80
appendFunction · 0.50
ReplaceMethod · 0.45
FindMethod · 0.45

Tested by

no test coverage detected