Created
March 8, 2017 23:46
-
-
Save mbildner/b26b49ebda70b0ebe0ad7cd90ef1f21c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
"github.com/PuerkitoBio/goquery" | |
"io/ioutil" | |
"os" | |
"regexp" | |
"strings" | |
) | |
func main() { | |
htmlBytes, err := ioutil.ReadAll(os.Stdin) | |
if err != nil { | |
fmt.Println("error reading from stdin!") | |
os.Exit(1) | |
} | |
query := os.Args[1] | |
if strings.HasPrefix(query, "attr") { | |
re := regexp.MustCompile(`"(.*)"`) | |
str := re.FindStringSubmatch(query)[1] | |
doc, _ := goquery.NewDocumentFromReader(bytes.NewBuffer(htmlBytes)) | |
q := doc.Find("body > *") | |
q.Each(func(_ int, s *goquery.Selection) { | |
attributeValue, exists := s.Attr(str) | |
if exists { | |
fmt.Println(attributeValue) | |
} | |
}) | |
} else { | |
doc, _ := goquery.NewDocumentFromReader(bytes.NewBuffer(htmlBytes)) | |
doc.Find(query).Each(func(_ int, s *goquery.Selection) { | |
html, _ := goquery.OuterHtml(s) | |
fmt.Println(html) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment