登录
首页 >  Golang >  Go问答

GO SDK 返回 HTML 而不是 JSON 用于 Google Translation API

来源:stackoverflow

时间:2024-02-25 22:30:27 229浏览 收藏

欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《GO SDK 返回 HTML 而不是 JSON 用于 Google Translation API》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!

问题内容

我使用以下代码进行翻译:

import (
    "context"

    "cloud.google.com/go/translate"
    "golang.org/x/text/language"
    "google.golang.org/api/option"
)

func translatefromgoogle(sourcearr []string, srclangcode, trgtlangcode, apikey string) (targetarr []translate.translation, err error) {
    src, err := language.parse(srclangcode)
    if err != nil {
        return targetarr, err
    }
    trgt, err := language.parse(trgtlangcode)
    if err != nil {
        return targetarr, err
    }
    ctx := context.background()
    opts := option.withapikey(apikey)
    c, err := translate.newclient(ctx, opts)
    if err != nil {
        return targetarr, err
    }
    defer c.close()
    targetarr, err = c.translate(ctx, sourcearr, trgt,
        &translate.options{
            source: src,
            format: translate.text,
        })
    return targetarr, err
}

在错误中,返回以下类型的数据:

googleapi: got http response code 400 with body: <!doctype html>\n<html lang=en>\n  <meta charset=utf-8>\n  <meta name=viewport content=\"initial-scale=1, minimum-scale=1, width=device-width\">\n  <title>error 400 (bad request)!!1</title>\n  <style>\n    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}\n  </style>\n  <a href=//www.google.com/><span id=logo aria-label=google></span></a>\n  <p><b>400.</b> <ins>that’s an error.</ins>\n  <p>your client has issued a malformed or illegal request.  <ins>that’s all we know.</ins>\n

但我只想要这样的文本:

Your client has issued a malformed or illegal request.

是我遗漏了什么还是 sdk 问题?如何获得所需的消息作为输出?


正确答案


该方法应该是解析错误输出并从中获取所需的消息。

package main

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

func main() {

    var s []string 
    
    string := "googleapi: got http response code 400 with body: <!doctype html>\n<html lang=en>\n  <meta charset=utf-8>\n  <meta name=viewport content=\"initial-scale=1, minimum-scale=1, width=device-width\">\n  <title>error 400 (bad request)!!1</title>\n  <style>\n    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}\n  </style>\n  <a href=//www.google.com/><span id=logo aria-label=google></span></a>\n  <p><b>400.</b> <ins>that’s an error.</ins>\n  <p>your client has issued a malformed or illegal request.  <ins>that’s all we know.</ins>\n"
    doc, err := html.parse(strings.newreader(string))
    if err != nil { 
        fmt.println(err)
    }
    
    var f func(*html.node)
    f = func(n *html.node) {
        
        if n.type == html.elementnode && n.data == "p" {
            text := &bytes.buffer{}
            collecttext(n, text)
            s = append(s, text.string())
        }
        for c := n.firstchild; c != nil; c = c.nextsibling {
            f(c)
        }
    }
    f(doc)
    
    fmt.println(s[1]) // return value
}

func collecttext(n *html.node, buf *bytes.buffer) {
    if n.type == html.textnode {
        buf.writestring(n.data)
    }
    for c := n.firstchild; c != nil; c = c.nextsibling {
        collecttext(c, buf)
    }
}

输出:

your client has issued a malformed or illegal request.  that’s all we know.

更新:要解释为什么应使用上述方法,您必须检查“翻译”功能的工作原理:

func (c *client) translate(ctx context.context, inputs []string, target language.tag, opts *options) ([]translation, error)

因此,如您所见,它返回翻译类型和错误类型:

// translation
type Translation struct {
    Text string
    Source language.Tag
    Model string
} 

// error
type error interface {
    Error() string
}

查看更多here

总之,您必须解析错误字符串才能获得所需的输出。

以上就是《GO SDK 返回 HTML 而不是 JSON 用于 Google Translation API》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>