How to use getHTMLAttr method of html Package

Best K6 code snippet using html.getHTMLAttr

piper.go

Source:piper.go Github

copy

Full Screen

1package gopiper2import (3 "bytes"4 "encoding/json"5 "errors"6 "regexp"7 "strconv"8 "strings"9 "github.com/PuerkitoBio/goquery"10 "github.com/bitly/go-simplejson"11)12const (13 // begin new version14 PT_INT = "int"15 PT_FLOAT = "float"16 PT_BOOL = "bool"17 PT_STRING = "string"18 PT_INT_ARRAY = "int-array"19 PT_FLOAT_ARRAY = "float-array"20 PT_BOOL_ARRAY = "bool-array"21 PT_STRING_ARRAY = "string-array"22 PT_MAP = "map"23 PT_ARRAY = "array"24 PT_JSON_VALUE = "json"25 PT_JSON_PARSE = "jsonparse"26 // end new version27 // begin compatible old version28 PT_TEXT = "text"29 PT_HREF = "href"30 PT_HTML = "html"31 PT_ATTR = `attr\[([\w\W]+)\]`32 PT_ATTR_ARRAY = `attr-array\[([\w\W]+)\]`33 PT_IMG_SRC = "src"34 PT_IMG_ALT = "alt"35 PT_TEXT_ARRAY = "text-array"36 PT_HREF_ARRAY = "href-array"37 PT_OUT_HTML = "outhtml"38 // end compatible old version39 PAGE_JSON = "json"40 PAGE_HTML = "html"41 PAGE_JS = "js"42 PAGE_XML = "xml"43 PAGE_TEXT = "text"44)45type PipeItem struct {46 Name string `json:"name,omitempty"`47 Selector string `json:"selector,omitempty"`48 Type string `json:"type"`49 Filter string `json:"filter,omitempty"`50 SubItem []PipeItem `json:"subitem,omitempty"`51}52type htmlselector struct {53 *goquery.Selection54 attr string55 selector string56}57func (p *PipeItem) PipeBytes(body []byte, pagetype string) (interface{}, error) {58 switch pagetype {59 case PAGE_HTML:60 doc, err := goquery.NewDocumentFromReader(bytes.NewReader(body))61 if err != nil {62 return nil, err63 }64 return p.pipeSelection(doc.Selection)65 case PAGE_JSON:66 return p.pipeJson(body)67 case PAGE_TEXT:68 return p.pipeText(body)69 }70 return nil, nil71}72func (p *PipeItem) parseRegexp(body string) (interface{}, error) {73 s := p.Selector[7:]74 exp, err := regexp.Compile(s)75 if err != nil {76 return nil, err77 }78 sv := exp.FindStringSubmatch(body)79 rs := ""80 if len(sv) == 1 {81 rs = sv[0]82 } else if len(sv) > 1 {83 rs = sv[1]84 sv = sv[1:]85 }86 switch p.Type {87 case PT_INT, PT_FLOAT, PT_BOOL:88 val, err := parseTextValue(rs, p.Type)89 if err != nil {90 return nil, err91 }92 return callFilter(val, p.Filter)93 case PT_INT_ARRAY, PT_FLOAT_ARRAY, PT_BOOL_ARRAY:94 val, err := parseTextValue(sv, p.Type)95 if err != nil {96 return nil, err97 }98 return callFilter(val, p.Filter)99 case PT_TEXT, PT_STRING:100 return callFilter(rs, p.Filter)101 case PT_TEXT_ARRAY, PT_STRING_ARRAY:102 return callFilter(sv, p.Filter)103 case PT_JSON_PARSE:104 if p.SubItem == nil || len(p.SubItem) <= 0 {105 return nil, errors.New("Pipe type jsonparse need one subItem!")106 }107 body, err := text2jsonbyte(rs)108 if err != nil {109 return nil, errors.New("jsonparse: text is not a json string" + err.Error())110 }111 parse_item := p.SubItem[0]112 res, err := parse_item.pipeJson(body)113 if err != nil {114 return nil, err115 }116 return callFilter(res, p.Filter)117 case PT_JSON_VALUE:118 res, err := text2json(rs)119 if err != nil {120 return nil, err121 }122 return callFilter(res, p.Filter)123 case PT_MAP:124 if p.SubItem == nil || len(p.SubItem) <= 0 {125 return nil, errors.New("Pipe type array need one subItem!")126 }127 res := make(map[string]interface{})128 for _, subitem := range p.SubItem {129 if subitem.Name == "" {130 continue131 }132 res[subitem.Name], _ = subitem.pipeText([]byte(rs))133 }134 return callFilter(res, p.Filter)135 }136 return nil, errors.New("Not support pipe type")137}138func (p *PipeItem) pipeSelection(s *goquery.Selection) (interface{}, error) {139 var (140 sel = htmlselector{s, "", p.Selector}141 err error142 )143 if strings.HasPrefix(p.Selector, "regexp:") {144 body, _ := sel.Html()145 return p.parseRegexp(body)146 }147 selector := p.Selector148 if selector != "" {149 sel, err = parseHtmlSelector(s, selector)150 if err != nil {151 return nil, err152 }153 selector = sel.selector154 }155 if sel.Size() == 0 {156 return nil, errors.New("Selector can't Find node!: " + selector)157 }158 attr_exp, _ := regexp.Compile(PT_ATTR)159 attr_array_exp, _ := regexp.Compile(PT_ATTR_ARRAY)160 if attr_exp.MatchString(p.Type) {161 vt := attr_exp.FindStringSubmatch(p.Type)162 res, has := sel.Attr(vt[1])163 if !has {164 return nil, errors.New("Can't Find attribute: " + p.Type + " selector: " + selector)165 }166 return callFilter(res, p.Filter)167 } else if attr_array_exp.MatchString(p.Type) {168 vt := attr_array_exp.FindStringSubmatch(p.Type)169 res := make([]string, 0)170 sel.Each(func(index int, child *goquery.Selection) {171 href, has := child.Attr(vt[1])172 if has {173 res = append(res, href)174 }175 })176 return callFilter(res, p.Filter)177 }178 switch p.Type {179 case PT_INT, PT_FLOAT, PT_BOOL, PT_STRING, PT_TEXT, PT_INT_ARRAY, PT_FLOAT_ARRAY, PT_BOOL_ARRAY, PT_STRING_ARRAY:180 val, err := parseHtmlAttr(sel, p.Type)181 if err != nil {182 return nil, err183 }184 return callFilter(val, p.Filter)185 case PT_HTML:186 html := ""187 sel.Each(func(idx int, s1 *goquery.Selection) {188 str, _ := s1.Html()189 html += str190 })191 return callFilter(html, p.Filter)192 case PT_OUT_HTML:193 html := ""194 sel.Each(func(idx int, s1 *goquery.Selection) {195 str, _ := goquery.OuterHtml(s1)196 html += str197 })198 return callFilter(html, p.Filter)199 case PT_HREF, PT_IMG_SRC, PT_IMG_ALT:200 res, has := sel.Attr(p.Type)201 if !has {202 return nil, errors.New("Can't Find attribute: " + p.Type + " selector: " + selector)203 }204 return callFilter(res, p.Filter)205 case PT_TEXT_ARRAY:206 res := make([]string, 0)207 sel.Each(func(index int, child *goquery.Selection) {208 res = append(res, child.Text())209 })210 return callFilter(res, p.Filter)211 case PT_HREF_ARRAY:212 res := make([]string, 0)213 sel.Each(func(index int, child *goquery.Selection) {214 href, has := child.Attr("href")215 if has {216 res = append(res, href)217 }218 })219 return callFilter(res, p.Filter)220 case PT_ARRAY:221 if p.SubItem == nil || len(p.SubItem) <= 0 {222 return nil, errors.New("Pipe type array need one subItem!")223 }224 array_item := p.SubItem[0]225 res := make([]interface{}, 0)226 sel.Each(func(index int, child *goquery.Selection) {227 v, _ := array_item.pipeSelection(child)228 res = append(res, v)229 })230 return callFilter(res, p.Filter)231 case PT_MAP:232 if p.SubItem == nil || len(p.SubItem) <= 0 {233 return nil, errors.New("Pipe type array need one subItem!")234 }235 res := make(map[string]interface{})236 for _, subitem := range p.SubItem {237 if subitem.Name == "" {238 continue239 }240 res[subitem.Name], _ = subitem.pipeSelection(sel.Selection)241 }242 return callFilter(res, p.Filter)243 default:244 return callFilter(0, p.Filter)245 }246 return nil, errors.New("Not support pipe type")247}248func parseHtmlSelector(s *goquery.Selection, selector string) (htmlselector, error) {249 attr := ""250 if selector == "" {251 return htmlselector{s, attr, selector}, nil252 }253 if idx := strings.Index(selector, "//"); idx > 0 {254 attr = strings.TrimSpace(selector[idx+2:])255 selector = strings.TrimSpace(selector[:idx])256 }257 subs := strings.Split(selector, "|")258 if len(subs) < 1 {259 return htmlselector{s.Find(selector), attr, selector}, nil260 }261 s = s.Find(subs[0])262 exp, _ := regexp.Compile(`([a-z_]+)(\(([\w\W+]+)\))?`)263 for i := 1; i < len(subs); i++ {264 if !exp.MatchString(subs[i]) {265 return htmlselector{s, attr, selector}, errors.New("error parse html selector: " + subs[i])266 }267 vt := exp.FindStringSubmatch(subs[i])268 fn := vt[1]269 params := ""270 if len(vt) > 3 {271 params = strings.TrimSpace(vt[3])272 }273 switch fn {274 case "eq":275 pm, _ := strconv.Atoi(params)276 s = s.Eq(pm)277 case "next":278 s = s.Next()279 case "prev":280 s = s.Prev()281 case "first":282 s = s.First()283 case "last":284 s = s.Last()285 case "siblings":286 s = s.Siblings()287 case "nextall":288 s = s.NextAll()289 case "children":290 s = s.Children()291 case "parent":292 s = s.Parent()293 case "parents":294 s = s.Parents()295 case "not":296 if params != "" {297 s = s.Not(params)298 }299 case "filter":300 if params != "" {301 s = s.Filter(params)302 }303 case "prevfilter":304 if params != "" {305 s = s.PrevFiltered(params)306 }307 case "prevallfilter":308 if params != "" {309 s = s.PrevAllFiltered(params)310 }311 case "nextfilter":312 if params != "" {313 s = s.NextFiltered(params)314 }315 case "nextallfilter":316 if params != "" {317 s = s.NextAllFiltered(params)318 }319 case "parentfilter":320 if params != "" {321 s = s.ParentFiltered(params)322 }323 case "parentsfilter":324 if params != "" {325 s = s.ParentsFiltered(params)326 }327 case "childrenfilter":328 if params != "" {329 s = s.ChildrenFiltered(params)330 }331 case "siblingsfilter":332 if params != "" {333 s = s.SiblingsFiltered(params)334 }335 case "rm":336 if params != "" {337 s.Find(params).Remove()338 }339 }340 }341 return htmlselector{s, attr, selector}, nil342}343func parseTextValue(text interface{}, tp string) (interface{}, error) {344 switch tp {345 case PT_INT, PT_INT_ARRAY:346 return text2int(text)347 case PT_FLOAT, PT_FLOAT_ARRAY:348 return text2float(text)349 case PT_BOOL, PT_BOOL_ARRAY:350 return text2bool(text)351 }352 return text, nil353}354func parseHtmlAttr(sel htmlselector, tp string) (interface{}, error) {355 switch tp {356 case PT_INT, PT_FLOAT, PT_BOOL, PT_TEXT, PT_STRING:357 text, err := gethtmlattr(sel.Selection, sel.attr, sel.selector)358 if err != nil {359 return nil, err360 }361 return parseTextValue(text, tp)362 case PT_INT_ARRAY, PT_FLOAT_ARRAY, PT_BOOL_ARRAY, PT_STRING_ARRAY:363 text, err := gethtmlattr_array(sel.Selection, sel.attr, sel.selector)364 if err != nil {365 return nil, err366 }367 return parseTextValue(text, tp)368 }369 return nil, errors.New("unknow html attr")370}371func gethtmlattr_array(sel *goquery.Selection, attr, selector string) ([]string, error) {372 res := make([]string, 0)373 if attr == "" {374 sel.Each(func(index int, child *goquery.Selection) {375 res = append(res, child.Text())376 })377 return res, nil378 }379 attr_exp, _ := regexp.Compile(PT_ATTR)380 if attr_exp.MatchString(attr) {381 vt := attr_exp.FindStringSubmatch(attr)382 sel.Each(func(index int, child *goquery.Selection) {383 text, has := child.Attr(vt[1])384 if has {385 res = append(res, text)386 }387 })388 return res, nil389 } else if attr == "html" {390 sel.Each(func(idx int, s1 *goquery.Selection) {391 str, _ := s1.Html()392 res = append(res, str)393 })394 return res, nil395 } else if attr == "outhtml" {396 sel.Each(func(idx int, s1 *goquery.Selection) {397 str, _ := goquery.OuterHtml(s1)398 res = append(res, str)399 })400 return res, nil401 }402 return res, nil403}404func gethtmlattr(sel *goquery.Selection, attr, selector string) (string, error) {405 if attr == "" {406 return sel.Text(), nil407 }408 attr_exp, _ := regexp.Compile(PT_ATTR)409 if attr_exp.MatchString(attr) {410 vt := attr_exp.FindStringSubmatch(attr)411 res, has := sel.Attr(vt[1])412 if !has {413 return "", errors.New("Can't Find attribute: " + attr + " selector: " + selector)414 }415 return res, nil416 } else if attr == "html" {417 html := ""418 sel.Each(func(idx int, s1 *goquery.Selection) {419 str, _ := s1.Html()420 html += str421 })422 return html, nil423 } else if attr == "outhtml" {424 html := ""425 sel.Each(func(idx int, s1 *goquery.Selection) {426 str, _ := goquery.OuterHtml(s1)427 html += str428 })429 return html, nil430 }431 return sel.Text(), nil432}433func parseJsonSelector(js *simplejson.Json, selector string) (*simplejson.Json, error) {434 subs := strings.Split(selector, ".")435 for _, s := range subs {436 if index := strings.Index(s, "["); index >= 0 {437 if index > 0 {438 k := s[:index]439 if k != "this" {440 js = js.Get(k)441 }442 }443 s = s[index:]444 exp, _ := regexp.Compile(`^\[(\d+)\]$`)445 if !exp.MatchString(s) {446 return nil, errors.New("parse json selector error: " + selector)447 }448 v := exp.FindStringSubmatch(s)449 int_v, err := strconv.Atoi(v[1])450 if err != nil {451 return nil, err452 }453 js = js.GetIndex(int_v)454 } else {455 if s == "this" {456 continue457 }458 js = js.Get(s)459 }460 }461 return js, nil462}463func (p *PipeItem) pipeJson(body []byte) (interface{}, error) {464 js, err := simplejson.NewJson(body)465 if err != nil {466 return nil, err467 }468 if p.Selector != "" {469 js, err = parseJsonSelector(js, p.Selector)470 if err != nil {471 return nil, err472 }473 }474 switch p.Type {475 case PT_INT:476 return callFilter(js.MustInt64(0), p.Filter)477 case PT_FLOAT:478 return callFilter(js.MustFloat64(0.0), p.Filter)479 case PT_BOOL:480 return callFilter(js.MustBool(false), p.Filter)481 case PT_TEXT, PT_STRING:482 return callFilter(js.MustString(""), p.Filter)483 case PT_TEXT_ARRAY, PT_STRING_ARRAY:484 v, err := js.StringArray()485 if err != nil {486 return nil, err487 }488 return callFilter(v, p.Filter)489 case PT_JSON_VALUE:490 return callFilter(js.Interface(), p.Filter)491 case PT_JSON_PARSE:492 if p.SubItem == nil || len(p.SubItem) <= 0 {493 return nil, errors.New("Pipe type jsonparse need one subItem!")494 }495 body_str := strings.TrimSpace(js.MustString(""))496 if body_str == "" {497 return nil, nil498 }499 body, err := text2jsonbyte(body_str)500 if err != nil {501 return nil, errors.New("jsonparse: text is not a json string" + err.Error())502 }503 parse_item := p.SubItem[0]504 res, err := parse_item.pipeJson(body)505 if err != nil {506 return nil, err507 }508 return callFilter(res, p.Filter)509 case PT_ARRAY:510 v, err := js.Array()511 if err != nil {512 return nil, err513 }514 if p.SubItem == nil || len(p.SubItem) <= 0 {515 return nil, errors.New("Pipe type array need one subItem!")516 }517 array_item := p.SubItem[0]518 res := make([]interface{}, 0)519 for _, r := range v {520 data, _ := json.Marshal(r)521 vl, _ := array_item.pipeJson(data)522 res = append(res, vl)523 }524 return callFilter(res, p.Filter)525 case PT_MAP:526 if p.SubItem == nil || len(p.SubItem) <= 0 {527 return nil, errors.New("Pipe type array need one subItem!")528 }529 data, _ := json.Marshal(js)530 res := make(map[string]interface{})531 for _, subitem := range p.SubItem {532 if subitem.Name == "" {533 continue534 }535 res[subitem.Name], _ = subitem.pipeJson(data)536 }537 return callFilter(res, p.Filter)538 default:539 return callFilter(0, p.Filter)540 }541 return nil, nil542}543func (p *PipeItem) pipeText(body []byte) (interface{}, error) {544 body_str := string(body)545 if strings.HasPrefix(p.Selector, "regexp:") {546 return p.parseRegexp(body_str)547 }548 switch p.Type {549 case PT_INT, PT_FLOAT, PT_BOOL:550 val, err := parseTextValue(body_str, p.Type)551 if err != nil {552 return nil, err553 }554 return callFilter(val, p.Filter)555 case PT_TEXT, PT_STRING:556 return callFilter(body_str, p.Filter)557 case PT_JSON_PARSE:558 if p.SubItem == nil || len(p.SubItem) <= 0 {559 return nil, errors.New("Pipe type jsonparse need one subItem!")560 }561 body, err := text2jsonbyte(body_str)562 if err != nil {563 return nil, errors.New("jsonparse: text is not a json string" + err.Error())564 }565 parse_item := p.SubItem[0]566 res, err := parse_item.pipeJson(body)567 if err != nil {568 return nil, err569 }570 return callFilter(res, p.Filter)571 case PT_JSON_VALUE:572 res, err := text2json(string(body))573 if err != nil {574 return nil, err575 }576 return callFilter(res, p.Filter)577 case PT_MAP:578 if p.SubItem == nil || len(p.SubItem) <= 0 {579 return nil, errors.New("Pipe type array need one subItem!")580 }581 res := make(map[string]interface{})582 for _, subitem := range p.SubItem {583 if subitem.Name == "" {584 continue585 }586 res[subitem.Name], _ = subitem.pipeText(body)587 }588 return callFilter(res, p.Filter)589 default:590 return callFilter(0, p.Filter)591 }592 return nil, errors.New("Not support pipe type")593}594func text2int(text interface{}) (interface{}, error) {595 switch val := text.(type) {596 case string:597 return strconv.ParseInt(val, 10, 64)598 case []string:599 vs := make([]int64, 0)600 for _, v := range val {601 n, err := strconv.ParseInt(v, 10, 64)602 if err != nil {603 return nil, err604 }605 vs = append(vs, n)606 }607 return vs, nil608 }609 return nil, errors.New("unsupport text2int type")610}611func text2float(text interface{}) (interface{}, error) {612 switch val := text.(type) {613 case string:614 return strconv.ParseFloat(val, 64)615 case []string:616 vs := make([]float64, 0)617 for _, v := range val {618 n, err := strconv.ParseFloat(v, 64)619 if err != nil {620 return nil, err621 }622 vs = append(vs, n)623 }624 return vs, nil625 }626 return nil, errors.New("unsupport text2float type")627}628func text2bool(text interface{}) (interface{}, error) {629 switch val := text.(type) {630 case string:631 return strconv.ParseBool(val)632 case []string:633 vs := make([]bool, 0)634 for _, v := range val {635 n, err := strconv.ParseBool(v)636 if err != nil {637 return nil, err638 }639 vs = append(vs, n)640 }641 return vs, nil642 }643 return nil, errors.New("unsupport text2bool type")644}645func text2json(text string) (interface{}, error) {646 res, err := textJsonValue(text)647 if err != nil {648 return untextJsonValue(text)649 }650 return res, nil651}652func text2jsonbyte(text string) ([]byte, error) {653 val, err := text2json(text)654 if err != nil {655 return nil, err656 }657 return json.Marshal(val)658}659func textJsonValue(text string) (interface{}, error) {660 res := map[string]interface{}{}661 if err := json.Unmarshal([]byte(text), &res); err != nil {662 resarray := make([]interface{}, 0)663 if err = json.Unmarshal([]byte(text), &resarray); err != nil {664 return nil, errors.New("parse json value error, text is not json value: " + err.Error())665 }666 return resarray, nil667 }668 return res, nil669}670func untextJsonValue(text string) (interface{}, error) {671 text, err := strconv.Unquote(`"` + text + `"`)672 if err != nil {673 return nil, err674 }675 return textJsonValue(text)676}...

Full Screen

Full Screen

e621api_html.go

Source:e621api_html.go Github

copy

Full Screen

1package e621api2import (3 "fmt"4 "log"5 "net/http"6 "strings"7 "golang.org/x/net/html"8)9//This file implements the e621.net API part that's not available over the official API (or at least now known to be available there) and so is read from HTML10func getHtmlAttr(attrs []html.Attribute, name string) string {11 for _, attr := range attrs {12 if attr.Key == name {13 return attr.Val14 }15 }16 return ""17}18type BlacklistEntry struct {19 original []string20 positive []string21 negative []string22}23func (be BlacklistEntry) String() string {24 return strings.Join(be.original, " ")25}26func createBlacklistEntry(beTags []string) (e BlacklistEntry) {27 e.original = beTags28 for _, blacklisted := range beTags {29 if blacklisted[0] == '-' {30 e.negative = append(e.negative, blacklisted[1:])31 } else {32 e.positive = append(e.positive, blacklisted)33 }34 }35 return36}37func (be *BlacklistEntry) matches(tags []string) bool {38positive_search:39 for _, blacklisted := range be.positive {40 //all positive tags must be contained41 for _, tag := range tags {42 if tag == blacklisted {43 continue positive_search44 }45 }46 //not found => at least this one is missing47 return false48 }49 for _, blacklisted := range be.negative {50 for _, tag := range tags {51 //any negative tag means it's not blacklisted52 if tag == blacklisted {53 return false54 }55 }56 }57 //every positive tag is contained and no negative tag => it's blacklisted58 return true59}60func (api *E621Api) GetDefaultBlacklist() (blacklistedTags []BlacklistEntry, error *E621Error) {61 path := "posts"62 requestUrl := fmt.Sprintf(E621Url+"%s", path)63 log.Println("Requesting", requestUrl)64 rq, err := http.NewRequest("GET", requestUrl, nil)65 if err != nil {66 error = &E621Error{"Error creating request", err}67 return68 }69 rq.Header.Add("User-Agent", "tgbt (by vaddux on e621)")70 rq.Header.Add("Accept", "text/html")71 r, err := api.client.Do(rq)72 if err != nil {73 error = &E621Error{"Error requesting", err}74 return75 }76 defer r.Body.Close()77 if r.StatusCode != 200 {78 error = &E621Error{"Response code", HttpStatus(r.StatusCode)}79 return80 }81 doc, err := html.Parse(r.Body)82 if err != nil {83 error = &E621Error{"Error parsing HTML", err}84 return85 }86 blacklistedTagsStr := ""87 var f func(*html.Node)88 f = func(n *html.Node) {89 if n.Type == html.ElementNode && n.Data == "meta" && getHtmlAttr(n.Attr, "name") == "blacklisted-tags" {90 blacklistedTagsStr = getHtmlAttr(n.Attr, "content")91 }92 for c := n.FirstChild; c != nil; c = c.NextSibling {93 f(c)94 }95 }96 f(doc)97 if blacklistedTagsStr == "" {98 error = &E621Error{"No meta[name='blacklisted-tags']", nil}99 return100 }101 var tagLists []string102 if err := deserialize(strings.NewReader(blacklistedTagsStr), &tagLists); err != nil {103 fmt.Printf("ERROR: %+v\n", err)104 error = &E621Error{"Error parsing meta[name='blacklisted-tags'].content", err}105 return106 }107 blacklistedTags = make([]BlacklistEntry, len(tagLists))108 for i := range tagLists {109 blacklistedTags[i] = createBlacklistEntry(strings.Fields(tagLists[i]))110 }111 return112}...

Full Screen

Full Screen

util.go

Source:util.go Github

copy

Full Screen

1package html2import (3 "encoding/json"4 "strconv"5 "strings"6 "github.com/PuerkitoBio/goquery"7 "github.com/dop251/goja"8 "github.com/serenize/snaker"9 gohtml "golang.org/x/net/html"10)11func attrToProperty(s string) string {12 if idx := strings.Index(s, "-"); idx != -1 {13 return s[0:idx] + snaker.SnakeToCamel(strings.Replace(s[idx+1:], "-", "_", -1))14 }15 return s16}17func propertyToAttr(attrName string) string {18 return strings.Replace(snaker.CamelToSnake(attrName), "_", "-", -1)19}20func namespaceURI(prefix string) string {21 switch prefix {22 case "svg":23 return "http://www.w3.org/2000/svg"24 case "math":25 return "http://www.w3.org/1998/Math/MathML"26 default:27 return "http://www.w3.org/1999/xhtml"28 }29}30func valueOrHTML(s *goquery.Selection) string {31 if val, exists := s.Attr("value"); exists {32 return val33 }34 if val, err := s.Html(); err == nil {35 return val36 }37 return ""38}39func getHtmlAttr(node *gohtml.Node, name string) *gohtml.Attribute {40 for i := 0; i < len(node.Attr); i++ {41 if node.Attr[i].Key == name {42 return &node.Attr[i]43 }44 }45 return nil46}47func elemList(s Selection) (items []goja.Value) {48 items = make([]goja.Value, s.Size())49 for i := 0; i < s.Size(); i++ {50 items[i] = selToElement(s.Eq(i))51 }52 return items53}54func nodeToElement(e Element, node *gohtml.Node) goja.Value {55 // Goquery does not expose a way to build a goquery.Selection with an arbitrary html.Node.56 // Workaround by adding a node to an empty Selection57 emptySel := e.sel.emptySelection()58 emptySel.sel.Nodes = append(emptySel.sel.Nodes, node)59 return selToElement(emptySel)60}61// Try to read numeric values in data- attributes.62// Return numeric value when the representation is unchanged by conversion to float and back.63// Other potentially numeric values (ie "101.00" "1E02") remain as strings.64func toNumeric(val string) (float64, bool) {65 if fltVal, err := strconv.ParseFloat(val, 64); err != nil {66 return 0, false67 } else if repr := strconv.FormatFloat(fltVal, 'f', -1, 64); repr == val {68 return fltVal, true69 } else {70 return 0, false71 }72}73func convertDataAttrVal(val string) interface{} {74 if len(val) == 0 {75 return goja.Undefined()76 } else if val[0] == '{' || val[0] == '[' {77 var subdata interface{}78 err := json.Unmarshal([]byte(val), &subdata)79 if err == nil {80 return subdata81 } else {82 return val83 }84 } else {85 switch val {86 case "true":87 return true88 case "false":89 return false90 case "null":91 return goja.Undefined()92 case "undefined":93 return goja.Undefined()94 default:95 if fltVal, isOk := toNumeric(val); isOk {96 return fltVal97 } else {98 return val99 }100 }101 }102}...

Full Screen

Full Screen

getHTMLAttr

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(html.GetHTMLAttr())4}5import (6func main() {7 fmt.Println(html.GetHTMLAttr())8}9import (10func main() {11 fmt.Println(html.GetHTMLAttr())12}13import (14func main() {15 fmt.Println(html.GetHTMLAttr())16}17import (18func main() {19 fmt.Println(html.GetHTMLAttr())20}21import (22func main() {23 fmt.Println(html.GetHTMLAttr())24}25import (26func main() {27 fmt.Println(html.GetHTMLAttr())28}29import (30func main() {31 fmt.Println(html.GetHTMLAttr())32}33import (34func main() {35 fmt.Println(html.GetHTMLAttr())36}37import (38func main() {39 fmt.Println(html.GetHTMLAttr())40}41import (42func main() {43 fmt.Println(html.GetHTMLAttr())44}45import (

Full Screen

Full Screen

getHTMLAttr

Using AI Code Generation

copy

Full Screen

1import (2type html struct {3}4func (h *html) getHTMLAttr(attr string) []string {5 var re = regexp.MustCompile(`\s*` + attr + `=(["'])(.*?)\1`)6 attrList = re.FindAllStringSubmatch(h.body, -1)7 for _, attr := range attrList {8 out = append(out, attr[2])9 }10}11func main() {12 if err != nil {13 fmt.Println(err)14 }15 defer resp.Body.Close()16 body, err := ioutil.ReadAll(resp.Body)17 if err != nil {18 fmt.Println(err)19 }20 h := html{body: string(body)}21 fmt.Println(strings.Join(h.getHTMLAttr("href"), ", "))22}

Full Screen

Full Screen

getHTMLAttr

Using AI Code Generation

copy

Full Screen

1import (2func main() {3}4import (5func main() {6}7import (8func main() {9}

Full Screen

Full Screen

getHTMLAttr

Using AI Code Generation

copy

Full Screen

1import (2func main() {3}4import (5func main() {6}7import (8func main() {9}10import (11func main() {12}13import (14func main() {15}16import (17func main() {18}19import (20func main() {21}

Full Screen

Full Screen

getHTMLAttr

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var htmlObj = html.HTML{htmlString}4 var attr = htmlObj.GetHTMLAttr("a", "href")5 fmt.Println(attr)6}

Full Screen

Full Screen

getHTMLAttr

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if len(os.Args) != 3 {4 }5 resp, err := http.Get(os.Args[1])6 if err != nil {7 log.Fatal(err)8 }9 defer resp.Body.Close()10 if resp.StatusCode != http.StatusOK {11 log.Fatal(resp.Status)12 }13 doc, err := html.Parse(resp.Body)14 if err != nil {15 log.Fatal(err)16 }17 fmt.Println(getHTMLAttr(doc, os.Args[2]))18}19func getHTMLAttr(n *html.Node, attr string) string {20 if n.Type == html.ElementNode {21 for _, a := range n.Attr {22 if a.Key == attr {23 }24 }25 }26 for c := n.FirstChild; c != nil; c = c.NextSibling {27 if v := getHTMLAttr(c, attr); v != "" {28 }29 }30}31import (32func main() {33 if len(os.Args) != 3 {34 }35 resp, err := http.Get(os.Args[1])36 if err != nil {37 log.Fatal(err)38 }39 defer resp.Body.Close()40 if resp.StatusCode != http.StatusOK {41 log.Fatal(resp.Status)42 }43 doc, err := html.Parse(resp.Body)44 if err != nil {45 log.Fatal(err)46 }47 fmt.Println(getHTMLAttr(doc, os.Args[2]))48}49func getHTMLAttr(n *html.Node, attr string) string {50 if n.Type == html.ElementNode {

Full Screen

Full Screen

getHTMLAttr

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 html := html.NewHTML()4 fmt.Println(html.GetHTMLAttr("a", "href"))5}6import (7func main() {8 html := html.NewHTML()9 fmt.Println(html.GetHTMLAttr("a", "href"))10}11import (12func main() {13 html := html.NewHTML()14 fmt.Println(html.GetHTMLAttr("a", "href"))15}16import (17func main() {18 html := html.NewHTML()19 fmt.Println(html.GetHTMLAttr("a", "href"))20}21import (22func main() {23 html := html.NewHTML()24 fmt.Println(html.GetHTMLAttr("a", "href"))25}26import (27func main() {28 html := html.NewHTML()

Full Screen

Full Screen

getHTMLAttr

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var html = HTML.HTML{Content: "<html><head><title>Test</title></head><body><h1>Heading</h1></body></html>"}4 fmt.Println(html.GetHTMLAttr("title"))5}6import (7func main() {8 var html = HTML.HTML{Content: "<html><head><title>Test</title></head><body><h1>Heading</h1></body></html>"}9 fmt.Println(html.GetHTMLAttr("title"))10}11import (12func main() {13 var html = HTML.HTML{Content: "<html><head><title>Test</title></head><body><h1>Heading</h1></body></html>"}14 fmt.Println(html.GetHTMLAttr("title"))15}16import (17func main() {18 var html = HTML.HTML{Content: "<html><head><title>Test</title></head><body><h1>Heading</h1></body></html>"}19 fmt.Println(html.GetHTMLAttr("title"))20}21import (22func main() {23 var html = HTML.HTML{Content: "<html><head><title>Test</title></head><body><h1>Heading</h1></body></html>"}24 fmt.Println(html.GetHTMLAttr("title"))25}26import (27func main() {28 var html = HTML.HTML{Content: "<html><head><title>Test</

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful