dongwo6477 2015-09-17 16:56
浏览 140
已采纳

从div获取没有子元素的文本

I'm trying to parse this html with the help of goquery. I can't figure out how to parse the string "The string I need" while throwing away everything else.

  <div class="outter-class">
        <h1 class="inner-class">
        The string I need
        <span class="other-class" >Some value I don't need</span>
        <span class="other-class2" title="sometitle"></span>
        </h1>
        <div class="other-class3">
            <h3>Some heading i don't need</h3>
        </div>
    </div>

I tried to use something like: https://stackoverflow.com/a/8851526/989919 by adapting it to goquery like that:

test := s.Clone().Children().Empty().End().Text()
fmt.Println(test.Text()) 

But that doesn't work. I tried a lot of different variants from the API but I can't figure it out.

  • 写回答

3条回答 默认 最新

  • douhui8163 2015-09-17 21:27
    关注

    The way I got it to work was with:

    // End() lets us jump back to the h1 selection to get its text
    text := doc.Find("h1").Children().Remove().End().Text()
    text = strings.TrimSpace(text)
    fmt.Println(text)
    

    Output:

    The string I need

    The code removes the child nodes (the span elements) from the h1 element to get the correct output. There may be an easier way of doing this, but it works. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输