如何从 HTML 中的嵌入式 Javascript 中抓取值?

我需要从网页中嵌入的 Javascript 中解析一些值。我试图用这样的东西标记 HTML,但它没有标记 Javascript 部分。


func CheckSitegroup(httpBody io.Reader) []string {

    sitegroups := make([]string, 0)

    page := html.NewTokenizer(httpBody)

    for {

        tokenType := page.Next()

        fmt.Println("TokenType:", tokenType)

        // check if HTML file has ended

        if tokenType == html.ErrorToken {

            return sitegroups

        }

        token := page.Token()

        fmt.Println("Token:", token)

        if tokenType == html.StartTagToken && token.DataAtom.String() == "script" {

            for _, attr := range token.Attr {

                fmt.Println("ATTR.KEY:", attr.Key)

                sitegroups = append(sitegroups, attr.Val)

            }

        }

    }

}

HTML 正文中的脚本如下所示,我需要广告系列编号(如果没有编号或根本没有 test.campaign =,则为 nil / "" - 站点组也是如此)。有没有简单的方法来获取信息?我想过正则表达式,但也许还有别的东西?从未使用过正则表达式。


<script type="text/javascript" >

    var test = {};

    test.campaign = "8d26113ba";

    test.isTest = "false";

    test.sitegroup = "Homepage";

</script>


偶然的你
浏览 161回答 2
2回答

饮歌长啸

Go 标准字符串库附带了许多有用的函数,您可以使用它们来解析 JavaScript 代码以获取所需的活动编号。以下代码可以从问题中提供的 js 代码中获取活动编号(在 Go Playground 上运行代码):package mainimport (&nbsp; &nbsp; "bufio"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "os"&nbsp; &nbsp; "strings")const js = `&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<script type="text/javascript" >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; var test = {};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; test.campaign = "8d26113ba";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; test.isTest = "false";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; test.sitegroup = "Homepage";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;`func StringToLines(s string) []string {&nbsp; &nbsp; var lines []string&nbsp; &nbsp; scanner := bufio.NewScanner(strings.NewReader(s))&nbsp; &nbsp; for scanner.Scan() {&nbsp; &nbsp; &nbsp; &nbsp; lines = append(lines, scanner.Text())&nbsp; &nbsp; }&nbsp; &nbsp; if err := scanner.Err(); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintln(os.Stderr, "reading standard input:", err)&nbsp; &nbsp; }&nbsp; &nbsp; return lines}func getCampaignNumber(line string) string {&nbsp; &nbsp; tmp := strings.Split(line, "=")[1]&nbsp; &nbsp; tmp = strings.TrimSpace(tmp)&nbsp; &nbsp; tmp = tmp[1 : len(tmp)-2]&nbsp; &nbsp; return tmp}func main() {&nbsp; &nbsp; lines := StringToLines(js)&nbsp; &nbsp; for _, line := range lines {&nbsp; &nbsp; &nbsp; &nbsp; if strings.Contains(line, "campaign") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result := getCampaignNumber(line)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; println(result)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go