add ability to scrap multiple cities

This commit is contained in:
2020-03-06 12:22:23 +11:00
parent 42c90f74b1
commit a4b7b014c4
2 changed files with 23 additions and 22 deletions
+23 -19
View File
@@ -20,15 +20,17 @@ import (
const (
urlTemplate = "https://www.domain.com.au/auction-results"
timeLayout = "2006-01-02"
city = "melbourne"
initDate = "2020-02-29"
clearanceInfoSelector = "table.css-54wo6m"
housesSelector = "article.css-3xqrp1"
scrapTimes = 150
)
var (
city string
auctionDate time.Time
isUpToDate = false
cities = []string{"melbourne", "sydney"}
)
func main() {
@@ -63,20 +65,20 @@ func main() {
}
latestStatus.Latest = e.Text
// handler.SaveStatus(latestStatus)
handler.SaveStatus(latestStatus)
})
c.OnHTML(clearanceInfoSelector, func(e *colly.HTMLElement) {
handlers.ScrapClearanceRate(city, auctionDate, db, e)
})
// c.OnHTML("article.css-3xqrp1", func(e *colly.HTMLElement) {
// if isUpToDate {
// return
// }
// handlers.ScrapHouses(city, day, db, e)
c.OnHTML(housesSelector, func(e *colly.HTMLElement) {
// if isUpToDate {
// return
// }
handlers.ScrapHouses(city, auctionDate, db, e)
// })
})
c.OnRequest(func(r *colly.Request) {
log.Info().Msgf("visiting %s", r.URL)
@@ -87,19 +89,21 @@ func main() {
if err != nil {
log.Panic().Msgf("failed to get date %v", err)
}
for i := 0; i < scrapTimes; i++ {
url := fmt.Sprintf("%s/%s/%s", urlTemplate, city, auctionDate.Format(timeLayout))
if err = c.Visit(url); err != nil {
if err.Error() == "Not Found" {
log.Warn().Msgf("Not fund data at %s with error %v will try next", url, err)
} else {
log.Panic().Msgf("failed to scrap content %v", err)
for _, cityVariable := range cities {
city = cityVariable
for i := 0; i < scrapTimes; i++ {
url := fmt.Sprintf("%s/%s/%s", urlTemplate, city, auctionDate.Format(timeLayout))
if err = c.Visit(url); err != nil {
if err.Error() == "Not Found" {
log.Warn().Msgf("Not fund data at %s with error %v will try next", url, err)
} else {
log.Panic().Msgf("failed to scrap content %v", err)
}
}
}
auctionDate = auctionDate.Add(-time.Hour * 24 * 7)
log.Info().Msgf("wil scrap the data for %s", auctionDate)
auctionDate = auctionDate.Add(-time.Hour * 24 * 7)
log.Info().Msgf("wil scrap the data for %s and url %s", auctionDate, url)
}
}
}
-3
View File
@@ -1,7 +1,6 @@
package utils
import (
"fmt"
"math"
"strconv"
"strings"
@@ -42,8 +41,6 @@ func ToMoney(priceString string) int {
floatPrice, err := strconv.ParseFloat(purePriceString, 10)
fmt.Println(floatPrice)
if err != nil {
log.Error().Msgf("cannot convert string to price data due to %v", err)
return 0