dream_life5200 2019-06-06 10:33
浏览 82

HTML到文本,例如Python的BeautifulSoup

I Have a Python program that gives the output as the following:

from bs4 import BeautifulSoup

html = `<h1>This is heading</h1> <p>this is parah <strong>strong</strong> that\'s how it works</p>`

parsed_html = BeautifulSoup(html, 'html.parser')
all_lines = parsed_html.findAll(text=True)
print(all_lines)

# ['This is heading', ' ', 'this is parah ', 'strong', " that's how it works"]

I am trying to achieve the same in golang but not able to get the required output. so far what I have tried:

import (
    "fmt"
    "strings"
    "github.com/PuerkitoBio/goquery"
)

func parseHTML(body string) string {

    p := strings.NewReader(body)
    doc, _ := goquery.NewDocumentFromReader(p)

    fmt.Println(doc.Text()) 

    // output: This is heading this is parah strong thats how it works

}
  • 写回答

1条回答 默认 最新

  • dptsivmg82908 2019-07-16 04:40
    关注

    Looks easy if you can implement a function on your own.

    Just remove all the tags"..."tags and keep appending the with "..."

    That will give you exactly the same as python output.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!