小编Ele*_*tal的帖子

Golang导入问题

我在导入“golang.org/x/net/html”时遇到问题,它只是告诉我找不到该包。环顾互联网让我更加困惑,有些人说你不能再使用它了,有些人说这样做:在 Go 中安装 exp/html,有些人说只需使用“golang.org/x/net” /html”工作得很好。

这只是我尝试运行的代码的一小部分:

package main


import (
    "fmt"
    "html"
    "net/http"
    "golang.org/x/net/html"
    "os"
    "strings"
)

// Helper function to pull the href attribute from a Token
func getHref(t html.Token) (ok bool, href string) {
    // Iterate over all of the Token's attributes until we find an "href"
    for _, a := range t.Attr {
        if a.Key == "href" {
            href = a.Val
            ok = true
        }
    }

    return
}
Run Code Online (Sandbox Code Playgroud)

它显然不会让我使用 html 令牌,因为我无法导入该包。

go

6
推荐指数
1
解决办法
9494
查看次数

golang无法写入文本文件:句柄无效

各位大家好,

我为很快就提出另一个问题而道歉,但最近Go似乎是一件又一件事.

我有一个工作的网页刮板(感谢大家的帮助),从这个维基页面抓取我想要的所有信息:http://monsterhunter.wikia.com/wiki/MH4U:_Item_List

然后它显示我想要的一切,没有打嗝.但是,当我去写.txt文件时,我得到一个错误说明:"0写mh4u.txt:句柄无效"

这是我目前的代码供参考:

package main

import (
    "fmt"
    "log"

    "github.com/PuerkitoBio/goquery"
    "os"
    "io"
)

func main() {

    filename := "mh4u.txt"
    file, err := os.Create(filename)

    if err!= nil {
        fmt.Println(err)
    }
    doc, err := goquery.NewDocument("http://www.ign.com/wikis/monster-hunter-4/Items")
    if err != nil {
        log.Fatal(err)
    }

    doc.Find("tbody").Each(func(i int, s *goquery.Selection) {

        s.Find("td").Each(func(j int, s2 *goquery.Selection) {

            if s3 := s2.Find("img"); s3 != nil && s3.Length() > 0 {
                return
            }

            fmt.Printf(s2.Text())
            n, err := io.WriteString(file, s2.Text())

            if err != …
Run Code Online (Sandbox Code Playgroud)

go

-1
推荐指数
1
解决办法
2007
查看次数

标签 统计

go ×2