Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangwei committed Jul 5, 2019
1 parent 2815d70 commit 85f3bfe
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
51 changes: 51 additions & 0 deletions db/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"getAwayBSG/configs"
"go.mongodb.org/mongo-driver/bson"
"strconv"
"strings"
)

Expand Down Expand Up @@ -40,6 +41,56 @@ func Update(link string, m bson.M) {
}

func AddZLItem(items []interface{}) {

for i := 0; i < len(items); i++ {
salary := items[i].(map[string]interface{})["salary"]
if salary != nil && salary != "薪资面议" {

K := strings.Index(salary.(string), "K")
k := strings.Index(salary.(string), "k")
Q := strings.Index(salary.(string), "千")

W := strings.Index(salary.(string), "W")
w := strings.Index(salary.(string), "w")
Wan := strings.Index(salary.(string), "万")

xishu := 0.0

if K > 0 || k > 0 || Q > 0 {
xishu = 1000
} else if W > 0 || w > 0 || Wan > 0 {
xishu = 10000
} else {
xishu = 1
}

salary = strings.Replace(salary.(string), "K", "", 2)
salary = strings.Replace(salary.(string), "W", "", 2)
salary = strings.Replace(salary.(string), "千", "", 2)
salary = strings.Replace(salary.(string), "万", "", 2)
minAndMax := strings.Split(salary.(string), "-")
fmt.Println(salary)
fmt.Println(minAndMax)
min, err := strconv.ParseFloat(minAndMax[0], 32)
if err != nil {
min = 0
}
max, err := strconv.ParseFloat(minAndMax[1], 32)
if err != nil {
max = 0
}
min = min * xishu
max = max * xishu
avg := (min + max) / 2

items[i].(map[string]interface{})["min"] = min
items[i].(map[string]interface{})["max"] = max
items[i].(map[string]interface{})["avg"] = avg

}

}

configInfo := configs.Config()
client := GetInstance().client
ctx := GetInstance().ctx
Expand Down
30 changes: 27 additions & 3 deletions lianjia.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,22 @@ func crawlerOneCity(cityUrl string) {
price := e.ChildText(".totalPrice")
price = strings.Replace(price, "万", "0000", 1)
//fmt.Println("总价:" + price)
iPrice, err := strconv.Atoi(price)
if err != nil {
iPrice = 0
}

unitPrice := e.ChildAttr(".unitPrice", "data-price")

//fmt.Println("每平米:" + unitPrice)
//fmt.Println(e.Text)
db.Add(bson.M{"Title": title, "TotalePrice": price, "UnitPrice": unitPrice, "Link": link, "listCrawlTime": time.Now()})

iUnitPrice, err := strconv.Atoi(unitPrice)
if err != nil {
iUnitPrice = 0
}

db.Add(bson.M{"Title": title, "TotalePrice": iPrice, "UnitPrice": iUnitPrice, "Link": link, "listCrawlTime": time.Now()})

})

Expand Down Expand Up @@ -102,7 +112,13 @@ func crawlDetail() (sucnum int) {
extensions.RandomUserAgent(c)
extensions.Referer(c)
c.OnHTML(".area .mainInfo", func(element *colly.HTMLElement) {
db.Update(element.Request.URL.String(), bson.M{"area": strings.Replace(element.Text, "平米", "", 1), "detailCrawlTime": time.Now()})
area := strings.Replace(element.Text, "平米", "", 1)
iArea, err := strconv.Atoi(area)
if err != nil {
iArea = 0
}

db.Update(element.Request.URL.String(), bson.M{"area": iArea, "detailCrawlTime": time.Now()})

})

Expand All @@ -119,7 +135,15 @@ func crawlDetail() (sucnum int) {

c.OnHTML(".transaction li", func(element *colly.HTMLElement) {
if element.ChildText("span:first-child") == "挂牌时间" {
db.Update(element.Request.URL.String(), bson.M{"guapaitime": element.ChildText("span:last-child"), "detailCrawlTime": time.Now()})

sGTime := element.ChildText("span:last-child")
ttime, err := time.Parse("2006-01-02", sGTime)

if err != nil {
ttime = time.Now()
}

db.Update(element.Request.URL.String(), bson.M{"guapaitime": ttime, "detailCrawlTime": time.Now()})
}
})

Expand Down

0 comments on commit 85f3bfe

Please sign in to comment.