Bläddra i källkod

get all info item

hocjooker@gmail.com 1 år sedan
förälder
incheckning
9cc34a9da4
1 ändrade filer med 41 tillägg och 3 borttagningar
  1. 41 3
      main.go

+ 41 - 3
main.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"log"
 	"net/http"
+	"regexp"
 
 	"github.com/PuerkitoBio/goquery"
 )
@@ -29,7 +30,14 @@ func main() {
 	cShortDesc := ".woocommerce-product-details__short-description"
 	cItemCategory := ".posted_in"
 	cItemTags := ".tagged_as"
-	cItemImages := ".woocommerce-product-gallery__image"
+	cItemImages := ".woocommerce-product-gallery__image img"
+	cItemTextDesc := "div.woocommerce-tabs"
+	// cItemOptions := "select#rating option"
+	cItemPrice := ".summary>.price .woocommerce-Price-amount.amount"
+	cEmail := "footer"
+	cRating := ".star-rating .rating"
+	cAuthor := ".product-brand a"
+	cVideo := "iframe"
 
 	// Use the Find method to select elements that match the css selector
 	// doc.Find(cItemName).Each(func(index int, element *goquery.Selection) {
@@ -43,8 +51,38 @@ func main() {
 	fmt.Println("ItemCategory: ", doc.Find(cItemCategory).First().Text())
 	fmt.Println("ItemTags: ", doc.Find(cItemTags).First().Text())
 
+	fmt.Println("TextDesc: ", doc.Find(cItemTextDesc).First().Text())
+	fmt.Println("ItemPice: ", doc.Find(cItemPrice).First().Text())
+	fmt.Println("Email: ", findEmail(doc.Find(cEmail).First().Text(), ""))
+	fmt.Println("Rating : ", doc.Find(cRating).First().Text())
+	fmt.Println("Author : ")
+	doc.Find(cAuthor).Each(func(index int, element *goquery.Selection) {
+		link, _ := element.Attr("href")
+		fmt.Println("====================")
+		fmt.Println("AuthorName: ", element.Text())
+		fmt.Printf("AuthorLink: %s\n", link)
+	})
+
+	vdos := []string{}
+	doc.Find(cVideo).Each(func(index int, element *goquery.Selection) {
+		embed, _ := element.Attr("src")
+		vdos = append(vdos, embed)
+	})
+	fmt.Printf("ItemImages: %s\n", vdos)
+
+	imgs := []string{}
 	doc.Find(cItemImages).Each(func(index int, element *goquery.Selection) {
-		href, _ := element.Attr("href")
-		fmt.Printf("ItemImages: %s\n", href)
+		img, _ := element.Attr("src")
+		imgs = append(imgs, img)
 	})
+
+	fmt.Printf("ItemImages: %s\n", imgs)
+
+}
+
+func findEmail(body string, doms string) (emails []string) {
+	r, _ := regexp.Compile(`[a-zA-Z0-9_+&*-]+(?:\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,24}`)
+	emails = append(emails, r.FindStringSubmatch(body)...)
+
+	return
 }