[Golang] Scrape JavaScript Code via strings Package


Scape JavaScript code to get campaign number via Go strings package - answer to Stack Overflow question: How can I scrap values from embedded Javascript in HTML?

Run Code on Go Playground

so31864758.go | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

const js = `
<script type="text/javascript" >
    var test = {};
    test.campaign = "8d26113ba";
    test.isTest = "false";
    test.sitegroup = "Homepage";
</script>
`

func StringToLines(s string) []string {
	var lines []string

	scanner := bufio.NewScanner(strings.NewReader(s))
	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}

	if err := scanner.Err(); err != nil {
		fmt.Fprintln(os.Stderr, "reading standard input:", err)
	}

	return lines
}

func getCampaignNumber(line string) string {
	tmp := strings.Split(line, "=")[1]
	tmp = strings.TrimSpace(tmp)
	tmp = tmp[1 : len(tmp)-2]
	return tmp
}

func main() {
	lines := StringToLines(js)
	for _, line := range lines {
		if strings.Contains(line, "campaign") {
			result := getCampaignNumber(line)
			println(result)
		}
	}
}

Tested on: Ubuntu Linux 15.10, Go 1.6.1.


References:

[1]How can I scrap values from embedded Javascript in HTML? - Stack Overflow
[2]Posts containing 'goquery' - Stack Overflow