golang USES regular expressions to parse web pages
- 2020-05-27 05:55:46
- OfStack
Cut the crap and go straight to the code:
package main
import (
"fmt"
"time"
"io/ioutil"
"net/http"
"regexp"
"strings"
)
func main() {
ip_pool := []string{
"172.16.1.128",
"172.16.1.129",
"172.16.1.131",
"172.16.1.132",
"172.16.1.133",
"172.16.1.134",
"172.16.1.135",
"172.16.1.136",
"172.16.1.137",
"172.16.1.138",
"172.16.1.190",
}
for {
for i:=0;i<len(ip_pool);i++ {
url := "http://"+ip_pool[i]+":10022"
//fmt.Println("-----------------",ip_pool[i],"---------")
get_url(url)
time.Sleep(1*time.Millisecond)
}
//time.Sleep(time.Second * 60)
}
}
func get_url(url string){
fmt.Println("----------",url,"----------------")
resp, err := http.Get(url)
if err != nil {
fmt.Println("http get error.")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("http read error")
}
src := string(body)
// will HTML All tags are converted to lowercase
re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
src = re.ReplaceAllStringFunc(src, strings.ToLower)
// extract table The label
re,_ = regexp.Compile("\\<!doc[\\S\\s]+?\\<table")
src = re.ReplaceAllString(src, "<table")
re,_ = regexp.Compile("</table\\>[\\S\\s]+?\\</html\\>")
src = re.ReplaceAllString(src, "</table>")
// Remove the space at the beginning of the line
re,_ = regexp.Compile("(\\<tr>)([\\S\\s\\<>\"\\d]+?)(\\</tr>)")
//src = re.ReplaceAllString(src, "$2")
src = re.ReplaceAllString(src, "$2]")
// To get rid of <> The label
re,_ = regexp.Compile("<[\\S\\s]+?>")
src = re.ReplaceAllString(src, "")
re,_ = regexp.Compile("\n")
src = re.ReplaceAllString(src, "")
re,_ = regexp.Compile("[ ]+")
src = re.ReplaceAllString(src, " ")
re,_ = regexp.Compile("]")
src = re.ReplaceAllString(src, "\n")
// become json 1 2 3 4
re,_ = regexp.Compile("(\\w*)(\\w{2}:\\w{2}:\\w{2}:\\w{2}:\\w{2}:\\w{2})([A-Za-z ]*)(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} )([V\\d\\.]* )(\\d{4}-\\d{2}-\\d{2}( \\d{2}:\\d{2}(:\\d{2})?)?)")
/* (((\\d{4}-\\d{2}-\\d{2})+ (\\d{2}:\\d{2}:\\d{2})*?)")
*/
src = re.ReplaceAllString(src, "$1,$2,$3,$4,$5,$6,")
//re,_ = regexp.Compile("(<[\\S\\s]*?\">)([\\S\\s]+?)(</t")
//src = re.ReplaceAllString(src, "$2,")
// re,_ = regexp.Compile("<[\\S\\s]+?>")
// src = re.ReplaceAllString(src, "")
//reg := regexp.MustCompile("([A-Za-z]+?)(\n[\\s]+)([\\d]+)")
// src = reg.ReplaceAllString(src, "$1:$3")
// Remove consecutive newlines
//re, _ = regexp.Compile("\\s{2,}")
//src = re.ReplaceAllString(src, "\n")
//re = regexp.MustCompile("\n\\d+")
//fmt.Println(re.ReplaceAllLiteralString("hello\n2\nwork", '\d'))
src = strings.Replace(src," Virtual machine name The virtual machine MAC Virtual machine state The heartbeat of time Engine version Library date Number of scanned samples " , "vm_name,vm_mac,vm_state,vm_heart,vm_eg,vm_av_db,vm_count",-1)
fmt.Println(src)
//reg, err := regexp.Compile("[[0-9A-Za-z]{2}:?]{6}")
//fmt.Printf("%q,%v\n", reg.FindString("00:16:3e:4a:29:35"), err)
// "Hello",
// text := "Hello\n123\nGo\n123"
// reg = regexp.MustCompile("([A-Za-z]+?)(\n)([\\d]+)")
// fmt.Printf("%q\n", reg.ReplaceAllString(text, "$3:$1"))
//fmt.Println(strings.TrimSpace(src))
// Get rid of STYLE
//re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>")
//src = re.ReplaceAllString(src, "")
// Get rid of SCRIPT
//re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
//src = re.ReplaceAllString(src, "")
// Remove all Angle brackets HTML Code, and replace it with a newline character
//re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
//src = re.ReplaceAllString(src, "\n")
// Remove consecutive newlines
//re, _ = regexp.Compile("\\s{2,}")
//src = re.ReplaceAllString(src, "\n")
//fmt.Println(strings.TrimSpace(src))
}
That's the code I'm going to share with you in this article. I hope you like it.