dongtingrun4973 2015-09-03 19:11
浏览 31
已采纳

如何在Gokogiri中将文本节点转换为字符串?

For my first programming attempt with Go I'm trying to automate the downloading of the lovely wallpapers from Psiu Puxa, saving the images with filenames based on titles in the posts in the HTML.

However, I haven't found how to get the value of a text node as a string.

Example HTML, simplified:

<div class="post">
    <a class="w-inline-block post-name-link" href="/posts/mars-30">
        <h4>#80 Martian Landscape</h4>
    </a>
</div>
<div class="post">
    <a class="w-inline-block post-name-link" href="#">
        <h4><strong>#79 MARTIAN terrain</strong></h4>
    </a>
</div>

My Go package:

package main

import (
    "fmt"
    "net/http"
    "io/ioutil"
    "github.com/moovweb/gokogiri"
)

func main() {
    resp, _ := http.Get("http://psiupuxa3.webflow.io/")
    page, _ := ioutil.ReadAll(resp.Body)
    resp.Body.Close()

    doc, _ := gokogiri.ParseHtml(page)
    res, _ := doc.Search("//div[@class='post']")
    defer doc.Free()

    for i := range res {
        postTitleRes, _ := res[i].Search("a[contains(@class,'post-name-link')]//text()")
        fmt.Printf("%T: %v
", postTitleRes, postTitleRes)
    }

}

Result:

[]xml.Node: [#80 Martian Landscape]
[]xml.Node: [#79 MARTIAN terrain]
[]xml.Node: [#78 MARTIAN TERRAIN]

How can I obtain #79 MARTIAN terrain, etc., as strings for later use when saving files?

I've tried postTitle := postTitleRes.String() but the method apparently isn't available for xml.Node. I've spent some time looking through Gokogiri's source code and have found methods/instructions on coercing to strings, but I'm quite lost and would appreciate any pointers.

  • 写回答

1条回答 默认 最新

  • dongmei3498 2015-09-03 19:42
    关注

    You've got an array of xml.Node structs there. You would need to access the nodes contained in that array.

    If you're sure you have one element then you can

    postTitleRes[0].Content()
    

    or to capture all of those nodes:

    for _, node := range postTitleRes {
        fmt.Printf("%T: %v
    ", node, node.Content())
    }
    

    You can see that the Content function should be available to you once you have a singular xml.Node. Definition.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿