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 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?