mirror of
https://github.com/wahyd4/scraper.git
synced 2026-08-08 20:57:28 +10:00
add ability to scrap multiple cities
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user