윤준석

ADD: Crawling item with selenium

get sold_info, price, thumbnailUrl, extraInfo and process that strings or integer values
......@@ -3,16 +3,23 @@ module joongna
go 1.17
require (
github.com/PuerkitoBio/goquery v1.8.0 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/bunsenapp/go-selenium v0.1.0 // indirect
github.com/caarlos0/env/v6 v6.9.1 // indirect
github.com/fedesog/webdriver v0.0.0-20180606182539-99f36c92eaef // indirect
github.com/joho/godotenv v1.4.0 // indirect
github.com/labstack/echo/v4 v4.7.2 // indirect
github.com/labstack/gommon v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/tebeka/selenium v0.9.9 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect
golang.org/x/text v0.3.7 // indirect
sourcegraph.com/sourcegraph/go-selenium v0.0.0-20170113155244-3da7d00aac9c // indirect
)
......
This diff is collapsed. Click to expand it.
......@@ -13,5 +13,4 @@ type ApiResponseItem struct {
Link string `json:"link"`
Description string `json:"description"`
CafeName string `json:"cafename"`
CafeUrl string `json:"cafeurl"`
}
......
......@@ -3,7 +3,7 @@ package model
type Item struct {
Platform string `json:"platform"`
Name string `json:"name"`
Price uint `json:"price"`
Price int `json:"price"`
ThumbnailUrl string `json:"thumbnailUrl"`
ItemUrl string `json:"itemUrl"`
ExtraInfo string `json:"extraInfo"`
......
package service
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"joongna/config"
......@@ -9,15 +11,45 @@ import (
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/fedesog/webdriver"
)
func GetItemByKeyword(keyword string) ([]model.Item, error) {
var items []model.Item
itemsInfo := getItemsInfoByKeyword(keyword)
for _, itemInfo := range itemsInfo {
if itemInfo.CafeName != "중고나라" {
continue
}
itemUrl := itemInfo.Link
sold, price, thumbnailUrl, extraInfo := crawlingNaverCafe(itemUrl)
if sold == "판매 완료" {
continue
}
item := model.Item{
Platform: "중고나라",
Name: itemInfo.Title,
Price: price,
ThumbnailUrl: thumbnailUrl,
ItemUrl: itemUrl,
ExtraInfo: extraInfo,
}
fmt.Println(item)
items = append(items, item)
}
return items, nil
}
func getItemsInfoByKeyword(keyword string) []model.ApiResponseItem {
encText := url.QueryEscape("중고나라" + keyword)
encText := url.QueryEscape("중고나라 " + keyword + " 판매중")
apiUrl := "https://openapi.naver.com/v1/search/cafearticle.json?query=" + encText + "&sort=sim"
req, err := http.NewRequest("GET", apiUrl, nil)
......@@ -47,3 +79,53 @@ func getItemsInfoByKeyword(keyword string) []model.ApiResponseItem {
}
return apiResponse.Items
}
func crawlingNaverCafe(cafeUrl string) (string, int, string, string) {
driver := webdriver.NewChromeDriver("./chromedriver")
err := driver.Start()
if err != nil {
log.Println(err)
}
desired := webdriver.Capabilities{"Platform": "MacOS"}
required := webdriver.Capabilities{}
session, err := driver.NewSession(desired, required)
if err != nil {
log.Println(err)
}
err = session.Url(cafeUrl)
if err != nil {
log.Println(err)
}
time.Sleep(time.Second * 1)
session.FocusOnFrame("cafe_main")
resp, err := session.Source()
html, err := goquery.NewDocumentFromReader(bytes.NewReader([]byte(resp)))
if err != nil {
log.Fatal(err)
}
sold := html.Find("div.sold_area").Text()
price := priceStringToInt(html.Find(".ProductPrice").Text())
thumbnailUrl, _ := html.Find("div.product_thumb img").Attr("src")
extraInfo := html.Find(".se-module-text").Text()
sold = strings.TrimSpace(sold)
thumbnailUrl = strings.TrimSpace(thumbnailUrl)
extraInfo = strings.TrimSpace(extraInfo)
return sold, price, thumbnailUrl, extraInfo
}
func priceStringToInt(priceString string) int {
strings.TrimSpace(priceString)
priceString = strings.ReplaceAll(priceString, "원", "")
priceString = strings.ReplaceAll(priceString, ",", "")
price, err := strconv.Atoi(priceString)
if err != nil {
log.Fatal(err)
}
return price
}
......